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> | |
2c67e9ac | 36 | #include <net/inet_ecn.h> |
1da177e4 LT |
37 | #include <net/route.h> |
38 | #include <net/xfrm.h> | |
dae50295 | 39 | #include <net/compat.h> |
dfd56b8b | 40 | #if IS_ENABLED(CONFIG_IPV6) |
1da177e4 LT |
41 | #include <net/transp_v6.h> |
42 | #endif | |
35ebf65e | 43 | #include <net/ip_fib.h> |
1da177e4 LT |
44 | |
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 | |
2c7946a7 | 53 | #define IP_CMSG_PASSSEC 32 |
e8b2dfe9 | 54 | #define IP_CMSG_ORIGDSTADDR 64 |
1da177e4 LT |
55 | |
56 | /* | |
57 | * SOL_IP control messages. | |
58 | */ | |
d826eb14 | 59 | #define PKTINFO_SKB_CB(__skb) ((struct in_pktinfo *)((__skb)->cb)) |
1da177e4 LT |
60 | |
61 | static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb) | |
62 | { | |
d826eb14 | 63 | struct in_pktinfo info = *PKTINFO_SKB_CB(skb); |
1da177e4 | 64 | |
eddc9ec5 | 65 | info.ipi_addr.s_addr = ip_hdr(skb)->daddr; |
1da177e4 LT |
66 | |
67 | put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info); | |
68 | } | |
69 | ||
70 | static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb) | |
71 | { | |
eddc9ec5 | 72 | int ttl = ip_hdr(skb)->ttl; |
1da177e4 LT |
73 | put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl); |
74 | } | |
75 | ||
76 | static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb) | |
77 | { | |
eddc9ec5 | 78 | put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos); |
1da177e4 LT |
79 | } |
80 | ||
81 | static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb) | |
82 | { | |
83 | if (IPCB(skb)->opt.optlen == 0) | |
84 | return; | |
85 | ||
eddc9ec5 ACM |
86 | put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen, |
87 | ip_hdr(skb) + 1); | |
1da177e4 LT |
88 | } |
89 | ||
90 | ||
91 | static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb) | |
92 | { | |
93 | unsigned char optbuf[sizeof(struct ip_options) + 40]; | |
5e73ea1a | 94 | struct ip_options *opt = (struct ip_options *)optbuf; |
1da177e4 LT |
95 | |
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 | ||
2c7946a7 CZ |
108 | static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb) |
109 | { | |
110 | char *secdata; | |
dc49c1f9 | 111 | u32 seclen, secid; |
2c7946a7 CZ |
112 | int err; |
113 | ||
dc49c1f9 CZ |
114 | err = security_socket_getpeersec_dgram(NULL, skb, &secid); |
115 | if (err) | |
116 | return; | |
117 | ||
118 | err = security_secid_to_secctx(secid, &secdata, &seclen); | |
2c7946a7 CZ |
119 | if (err) |
120 | return; | |
121 | ||
122 | put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata); | |
dc49c1f9 | 123 | security_release_secctx(secdata, seclen); |
2c7946a7 CZ |
124 | } |
125 | ||
21d1a161 | 126 | static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb) |
e8b2dfe9 BS |
127 | { |
128 | struct sockaddr_in sin; | |
b71d1d42 | 129 | const struct iphdr *iph = ip_hdr(skb); |
21d1a161 | 130 | __be16 *ports = (__be16 *)skb_transport_header(skb); |
e8b2dfe9 BS |
131 | |
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 | } | |
1da177e4 LT |
147 | |
148 | void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb) | |
149 | { | |
150 | struct inet_sock *inet = inet_sk(skb->sk); | |
95c96174 | 151 | unsigned int flags = inet->cmsg_flags; |
1da177e4 LT |
152 | |
153 | /* Ordered by supposed usage frequency */ | |
154 | if (flags & 1) | |
155 | ip_cmsg_recv_pktinfo(msg, skb); | |
4d52cfbe | 156 | if ((flags >>= 1) == 0) |
1da177e4 LT |
157 | return; |
158 | ||
159 | if (flags & 1) | |
160 | ip_cmsg_recv_ttl(msg, skb); | |
4d52cfbe | 161 | if ((flags >>= 1) == 0) |
1da177e4 LT |
162 | return; |
163 | ||
164 | if (flags & 1) | |
165 | ip_cmsg_recv_tos(msg, skb); | |
4d52cfbe | 166 | if ((flags >>= 1) == 0) |
1da177e4 LT |
167 | return; |
168 | ||
169 | if (flags & 1) | |
170 | ip_cmsg_recv_opts(msg, skb); | |
4d52cfbe | 171 | if ((flags >>= 1) == 0) |
1da177e4 LT |
172 | return; |
173 | ||
174 | if (flags & 1) | |
175 | ip_cmsg_recv_retopts(msg, skb); | |
4d52cfbe | 176 | if ((flags >>= 1) == 0) |
2c7946a7 CZ |
177 | return; |
178 | ||
179 | if (flags & 1) | |
180 | ip_cmsg_recv_security(msg, skb); | |
e8b2dfe9 | 181 | |
4d52cfbe | 182 | if ((flags >>= 1) == 0) |
e8b2dfe9 BS |
183 | return; |
184 | if (flags & 1) | |
185 | ip_cmsg_recv_dstaddr(msg, skb); | |
186 | ||
1da177e4 | 187 | } |
4d52cfbe | 188 | EXPORT_SYMBOL(ip_cmsg_recv); |
1da177e4 | 189 | |
7a6adb92 | 190 | int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc) |
1da177e4 LT |
191 | { |
192 | int err; | |
193 | 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)); | |
4d52cfbe ED |
203 | err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg), |
204 | err < 40 ? err : 40); | |
1da177e4 LT |
205 | 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 | } | |
218 | default: | |
219 | return -EINVAL; | |
220 | } | |
221 | } | |
222 | return 0; | |
223 | } | |
224 | ||
225 | ||
226 | /* Special input handler for packets caught by router alert option. | |
227 | They are selected only by protocol field, and then processed likely | |
228 | local ones; but only if someone wants them! Otherwise, router | |
229 | not running rsvpd will kill RSVP. | |
230 | ||
231 | It is user level problem, what it will make with them. | |
232 | I have no idea, how it will masquearde or NAT them (it is joke, joke :-)), | |
233 | but receiver should be enough clever f.e. to forward mtrace requests, | |
234 | sent to multicast group to reach destination designated router. | |
235 | */ | |
43a951e9 | 236 | struct ip_ra_chain __rcu *ip_ra_chain; |
66018506 ED |
237 | static DEFINE_SPINLOCK(ip_ra_lock); |
238 | ||
592fcb9d ED |
239 | |
240 | static void ip_ra_destroy_rcu(struct rcu_head *head) | |
66018506 | 241 | { |
592fcb9d ED |
242 | struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu); |
243 | ||
244 | sock_put(ra->saved_sk); | |
245 | kfree(ra); | |
66018506 | 246 | } |
1da177e4 | 247 | |
4d52cfbe ED |
248 | int ip_ra_control(struct sock *sk, unsigned char on, |
249 | void (*destructor)(struct sock *)) | |
1da177e4 | 250 | { |
43a951e9 ED |
251 | struct ip_ra_chain *ra, *new_ra; |
252 | struct ip_ra_chain __rcu **rap; | |
1da177e4 | 253 | |
c720c7e8 | 254 | if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW) |
1da177e4 LT |
255 | return -EINVAL; |
256 | ||
257 | new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL; | |
258 | ||
66018506 | 259 | spin_lock_bh(&ip_ra_lock); |
43a951e9 ED |
260 | for (rap = &ip_ra_chain; |
261 | (ra = rcu_dereference_protected(*rap, | |
262 | lockdep_is_held(&ip_ra_lock))) != NULL; | |
263 | rap = &ra->next) { | |
1da177e4 LT |
264 | if (ra->sk == sk) { |
265 | if (on) { | |
66018506 | 266 | spin_unlock_bh(&ip_ra_lock); |
a51482bd | 267 | kfree(new_ra); |
1da177e4 LT |
268 | return -EADDRINUSE; |
269 | } | |
592fcb9d ED |
270 | /* dont let ip_call_ra_chain() use sk again */ |
271 | ra->sk = NULL; | |
66018506 ED |
272 | rcu_assign_pointer(*rap, ra->next); |
273 | spin_unlock_bh(&ip_ra_lock); | |
1da177e4 LT |
274 | |
275 | if (ra->destructor) | |
276 | ra->destructor(sk); | |
592fcb9d ED |
277 | /* |
278 | * Delay sock_put(sk) and kfree(ra) after one rcu grace | |
279 | * period. This guarantee ip_call_ra_chain() dont need | |
280 | * to mess with socket refcounts. | |
281 | */ | |
282 | ra->saved_sk = sk; | |
283 | call_rcu(&ra->rcu, ip_ra_destroy_rcu); | |
1da177e4 LT |
284 | return 0; |
285 | } | |
286 | } | |
287 | if (new_ra == NULL) { | |
66018506 | 288 | spin_unlock_bh(&ip_ra_lock); |
1da177e4 LT |
289 | return -ENOBUFS; |
290 | } | |
291 | new_ra->sk = sk; | |
292 | new_ra->destructor = destructor; | |
293 | ||
294 | new_ra->next = ra; | |
66018506 | 295 | rcu_assign_pointer(*rap, new_ra); |
1da177e4 | 296 | sock_hold(sk); |
66018506 | 297 | spin_unlock_bh(&ip_ra_lock); |
1da177e4 LT |
298 | |
299 | return 0; | |
300 | } | |
301 | ||
e905a9ed | 302 | void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err, |
35986b32 | 303 | __be16 port, u32 info, u8 *payload) |
1da177e4 | 304 | { |
1da177e4 LT |
305 | struct sock_exterr_skb *serr; |
306 | ||
1da177e4 LT |
307 | skb = skb_clone(skb, GFP_ATOMIC); |
308 | if (!skb) | |
309 | return; | |
310 | ||
e905a9ed | 311 | serr = SKB_EXT_ERR(skb); |
1da177e4 LT |
312 | serr->ee.ee_errno = err; |
313 | serr->ee.ee_origin = SO_EE_ORIGIN_ICMP; | |
88c7664f ACM |
314 | serr->ee.ee_type = icmp_hdr(skb)->type; |
315 | serr->ee.ee_code = icmp_hdr(skb)->code; | |
1da177e4 LT |
316 | serr->ee.ee_pad = 0; |
317 | serr->ee.ee_info = info; | |
318 | serr->ee.ee_data = 0; | |
88c7664f | 319 | serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) - |
d56f90a7 | 320 | skb_network_header(skb); |
1da177e4 LT |
321 | serr->port = port; |
322 | ||
bd82393c ACM |
323 | if (skb_pull(skb, payload - skb->data) != NULL) { |
324 | skb_reset_transport_header(skb); | |
325 | if (sock_queue_err_skb(sk, skb) == 0) | |
326 | return; | |
327 | } | |
328 | kfree_skb(skb); | |
1da177e4 LT |
329 | } |
330 | ||
0579016e | 331 | void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info) |
1da177e4 LT |
332 | { |
333 | struct inet_sock *inet = inet_sk(sk); | |
334 | struct sock_exterr_skb *serr; | |
335 | struct iphdr *iph; | |
336 | struct sk_buff *skb; | |
337 | ||
338 | if (!inet->recverr) | |
339 | return; | |
340 | ||
341 | skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC); | |
342 | if (!skb) | |
343 | return; | |
344 | ||
2ca9e6f2 ACM |
345 | skb_put(skb, sizeof(struct iphdr)); |
346 | skb_reset_network_header(skb); | |
eddc9ec5 | 347 | iph = ip_hdr(skb); |
1da177e4 LT |
348 | iph->daddr = daddr; |
349 | ||
e905a9ed | 350 | serr = SKB_EXT_ERR(skb); |
1da177e4 LT |
351 | serr->ee.ee_errno = err; |
352 | serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL; | |
e905a9ed | 353 | serr->ee.ee_type = 0; |
1da177e4 LT |
354 | serr->ee.ee_code = 0; |
355 | serr->ee.ee_pad = 0; | |
356 | serr->ee.ee_info = info; | |
357 | serr->ee.ee_data = 0; | |
d56f90a7 | 358 | serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb); |
1da177e4 LT |
359 | serr->port = port; |
360 | ||
27a884dc | 361 | __skb_pull(skb, skb_tail_pointer(skb) - skb->data); |
bd82393c | 362 | skb_reset_transport_header(skb); |
1da177e4 LT |
363 | |
364 | if (sock_queue_err_skb(sk, skb)) | |
365 | kfree_skb(skb); | |
366 | } | |
367 | ||
e905a9ed | 368 | /* |
1da177e4 LT |
369 | * Handle MSG_ERRQUEUE |
370 | */ | |
08c62a10 | 371 | int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len) |
1da177e4 LT |
372 | { |
373 | struct sock_exterr_skb *serr; | |
374 | struct sk_buff *skb, *skb2; | |
375 | struct sockaddr_in *sin; | |
376 | struct { | |
377 | struct sock_extended_err ee; | |
378 | struct sockaddr_in offender; | |
379 | } errhdr; | |
380 | int err; | |
381 | int copied; | |
382 | ||
383 | err = -EAGAIN; | |
384 | skb = skb_dequeue(&sk->sk_error_queue); | |
385 | if (skb == NULL) | |
386 | goto out; | |
387 | ||
388 | copied = skb->len; | |
389 | if (copied > len) { | |
390 | msg->msg_flags |= MSG_TRUNC; | |
391 | copied = len; | |
392 | } | |
393 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); | |
394 | if (err) | |
395 | goto out_free_skb; | |
396 | ||
397 | sock_recv_timestamp(msg, sk, skb); | |
398 | ||
399 | serr = SKB_EXT_ERR(skb); | |
400 | ||
401 | sin = (struct sockaddr_in *)msg->msg_name; | |
402 | if (sin) { | |
403 | sin->sin_family = AF_INET; | |
d56f90a7 ACM |
404 | sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) + |
405 | serr->addr_offset); | |
1da177e4 LT |
406 | sin->sin_port = serr->port; |
407 | memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); | |
08c62a10 | 408 | *addr_len = sizeof(*sin); |
1da177e4 LT |
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 | |
0c9f79be XW |
461 | switch (optname) { |
462 | case IP_PKTINFO: | |
463 | case IP_RECVTTL: | |
464 | case IP_RECVOPTS: | |
465 | case IP_RECVTOS: | |
466 | case IP_RETOPTS: | |
467 | case IP_TOS: | |
468 | case IP_TTL: | |
469 | case IP_HDRINCL: | |
470 | case IP_MTU_DISCOVER: | |
471 | case IP_RECVERR: | |
472 | case IP_ROUTER_ALERT: | |
473 | case IP_FREEBIND: | |
474 | case IP_PASSSEC: | |
475 | case IP_TRANSPARENT: | |
476 | case IP_MINTTL: | |
477 | case IP_NODEFRAG: | |
478 | case IP_UNICAST_IF: | |
479 | case IP_MULTICAST_TTL: | |
480 | case IP_MULTICAST_ALL: | |
481 | case IP_MULTICAST_LOOP: | |
482 | case IP_RECVORIGDSTADDR: | |
1da177e4 LT |
483 | if (optlen >= sizeof(int)) { |
484 | if (get_user(val, (int __user *) optval)) | |
485 | return -EFAULT; | |
486 | } else if (optlen >= sizeof(char)) { | |
487 | unsigned char ucval; | |
488 | ||
489 | if (get_user(ucval, (unsigned char __user *) optval)) | |
490 | return -EFAULT; | |
491 | val = (int) ucval; | |
492 | } | |
493 | } | |
494 | ||
495 | /* If optlen==0, it is equivalent to val == 0 */ | |
496 | ||
6a9fb947 | 497 | if (ip_mroute_opt(optname)) |
09cb105e | 498 | return ip_mroute_setsockopt(sk, optname, optval, optlen); |
1da177e4 LT |
499 | |
500 | err = 0; | |
501 | lock_sock(sk); | |
502 | ||
503 | switch (optname) { | |
132adf54 SH |
504 | case IP_OPTIONS: |
505 | { | |
f6d8bd05 ED |
506 | struct ip_options_rcu *old, *opt = NULL; |
507 | ||
65a1c4ff | 508 | if (optlen > 40) |
132adf54 | 509 | goto e_inval; |
3b1e0a65 | 510 | err = ip_options_get_from_user(sock_net(sk), &opt, |
cb84663e | 511 | optval, optlen); |
132adf54 SH |
512 | if (err) |
513 | break; | |
f6d8bd05 ED |
514 | old = rcu_dereference_protected(inet->inet_opt, |
515 | sock_owned_by_user(sk)); | |
132adf54 SH |
516 | if (inet->is_icsk) { |
517 | struct inet_connection_sock *icsk = inet_csk(sk); | |
dfd56b8b | 518 | #if IS_ENABLED(CONFIG_IPV6) |
132adf54 SH |
519 | if (sk->sk_family == PF_INET || |
520 | (!((1 << sk->sk_state) & | |
521 | (TCPF_LISTEN | TCPF_CLOSE)) && | |
c720c7e8 | 522 | inet->inet_daddr != LOOPBACK4_IPV6)) { |
1da177e4 | 523 | #endif |
f6d8bd05 ED |
524 | if (old) |
525 | icsk->icsk_ext_hdr_len -= old->opt.optlen; | |
132adf54 | 526 | if (opt) |
f6d8bd05 | 527 | icsk->icsk_ext_hdr_len += opt->opt.optlen; |
132adf54 | 528 | icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie); |
dfd56b8b | 529 | #if IS_ENABLED(CONFIG_IPV6) |
1da177e4 | 530 | } |
132adf54 | 531 | #endif |
1da177e4 | 532 | } |
f6d8bd05 ED |
533 | rcu_assign_pointer(inet->inet_opt, opt); |
534 | if (old) | |
605b4afe | 535 | kfree_rcu(old, rcu); |
132adf54 SH |
536 | break; |
537 | } | |
538 | case IP_PKTINFO: | |
539 | if (val) | |
540 | inet->cmsg_flags |= IP_CMSG_PKTINFO; | |
541 | else | |
542 | inet->cmsg_flags &= ~IP_CMSG_PKTINFO; | |
543 | break; | |
544 | case IP_RECVTTL: | |
545 | if (val) | |
546 | inet->cmsg_flags |= IP_CMSG_TTL; | |
547 | else | |
548 | inet->cmsg_flags &= ~IP_CMSG_TTL; | |
549 | break; | |
550 | case IP_RECVTOS: | |
551 | if (val) | |
552 | inet->cmsg_flags |= IP_CMSG_TOS; | |
553 | else | |
554 | inet->cmsg_flags &= ~IP_CMSG_TOS; | |
555 | break; | |
556 | case IP_RECVOPTS: | |
557 | if (val) | |
558 | inet->cmsg_flags |= IP_CMSG_RECVOPTS; | |
559 | else | |
560 | inet->cmsg_flags &= ~IP_CMSG_RECVOPTS; | |
561 | break; | |
562 | case IP_RETOPTS: | |
563 | if (val) | |
564 | inet->cmsg_flags |= IP_CMSG_RETOPTS; | |
565 | else | |
566 | inet->cmsg_flags &= ~IP_CMSG_RETOPTS; | |
567 | break; | |
568 | case IP_PASSSEC: | |
569 | if (val) | |
570 | inet->cmsg_flags |= IP_CMSG_PASSSEC; | |
571 | else | |
572 | inet->cmsg_flags &= ~IP_CMSG_PASSSEC; | |
573 | break; | |
e8b2dfe9 BS |
574 | case IP_RECVORIGDSTADDR: |
575 | if (val) | |
576 | inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR; | |
577 | else | |
578 | inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR; | |
579 | break; | |
132adf54 SH |
580 | case IP_TOS: /* This sets both TOS and Precedence */ |
581 | if (sk->sk_type == SOCK_STREAM) { | |
2c67e9ac MŻ |
582 | val &= ~INET_ECN_MASK; |
583 | val |= inet->tos & INET_ECN_MASK; | |
132adf54 | 584 | } |
132adf54 SH |
585 | if (inet->tos != val) { |
586 | inet->tos = val; | |
587 | sk->sk_priority = rt_tos2priority(val); | |
588 | sk_dst_reset(sk); | |
589 | } | |
590 | break; | |
591 | case IP_TTL: | |
4d52cfbe | 592 | if (optlen < 1) |
132adf54 | 593 | goto e_inval; |
c9be4a5c | 594 | if (val != -1 && (val < 1 || val > 255)) |
132adf54 SH |
595 | goto e_inval; |
596 | inet->uc_ttl = val; | |
597 | break; | |
598 | case IP_HDRINCL: | |
599 | if (sk->sk_type != SOCK_RAW) { | |
600 | err = -ENOPROTOOPT; | |
2c7946a7 | 601 | break; |
132adf54 SH |
602 | } |
603 | inet->hdrincl = val ? 1 : 0; | |
604 | break; | |
7b2ff18e JO |
605 | case IP_NODEFRAG: |
606 | if (sk->sk_type != SOCK_RAW) { | |
607 | err = -ENOPROTOOPT; | |
608 | break; | |
609 | } | |
610 | inet->nodefrag = val ? 1 : 0; | |
611 | break; | |
132adf54 | 612 | case IP_MTU_DISCOVER: |
0eae750e | 613 | if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_PROBE) |
132adf54 SH |
614 | goto e_inval; |
615 | inet->pmtudisc = val; | |
616 | break; | |
617 | case IP_RECVERR: | |
618 | inet->recverr = !!val; | |
619 | if (!val) | |
620 | skb_queue_purge(&sk->sk_error_queue); | |
621 | break; | |
622 | case IP_MULTICAST_TTL: | |
623 | if (sk->sk_type == SOCK_STREAM) | |
624 | goto e_inval; | |
4d52cfbe | 625 | if (optlen < 1) |
132adf54 | 626 | goto e_inval; |
09cb105e | 627 | if (val == -1) |
132adf54 SH |
628 | val = 1; |
629 | if (val < 0 || val > 255) | |
630 | goto e_inval; | |
631 | inet->mc_ttl = val; | |
632 | break; | |
633 | case IP_MULTICAST_LOOP: | |
4d52cfbe | 634 | if (optlen < 1) |
132adf54 SH |
635 | goto e_inval; |
636 | inet->mc_loop = !!val; | |
637 | break; | |
76e21053 EH |
638 | case IP_UNICAST_IF: |
639 | { | |
640 | struct net_device *dev = NULL; | |
641 | int ifindex; | |
642 | ||
643 | if (optlen != sizeof(int)) | |
644 | goto e_inval; | |
645 | ||
646 | ifindex = (__force int)ntohl((__force __be32)val); | |
647 | if (ifindex == 0) { | |
648 | inet->uc_index = 0; | |
649 | err = 0; | |
650 | break; | |
651 | } | |
652 | ||
653 | dev = dev_get_by_index(sock_net(sk), ifindex); | |
654 | err = -EADDRNOTAVAIL; | |
655 | if (!dev) | |
656 | break; | |
657 | dev_put(dev); | |
658 | ||
659 | err = -EINVAL; | |
660 | if (sk->sk_bound_dev_if) | |
661 | break; | |
662 | ||
663 | inet->uc_index = ifindex; | |
664 | err = 0; | |
665 | break; | |
666 | } | |
132adf54 SH |
667 | case IP_MULTICAST_IF: |
668 | { | |
669 | struct ip_mreqn mreq; | |
670 | struct net_device *dev = NULL; | |
671 | ||
672 | if (sk->sk_type == SOCK_STREAM) | |
673 | goto e_inval; | |
674 | /* | |
675 | * Check the arguments are allowable | |
676 | */ | |
677 | ||
0915921b SW |
678 | if (optlen < sizeof(struct in_addr)) |
679 | goto e_inval; | |
680 | ||
132adf54 SH |
681 | err = -EFAULT; |
682 | if (optlen >= sizeof(struct ip_mreqn)) { | |
09cb105e | 683 | if (copy_from_user(&mreq, optval, sizeof(mreq))) |
1da177e4 | 684 | break; |
132adf54 SH |
685 | } else { |
686 | memset(&mreq, 0, sizeof(mreq)); | |
3a084ddb JP |
687 | if (optlen >= sizeof(struct ip_mreq)) { |
688 | if (copy_from_user(&mreq, optval, | |
689 | sizeof(struct ip_mreq))) | |
690 | break; | |
691 | } else if (optlen >= sizeof(struct in_addr)) { | |
692 | if (copy_from_user(&mreq.imr_address, optval, | |
693 | sizeof(struct in_addr))) | |
694 | break; | |
695 | } | |
132adf54 SH |
696 | } |
697 | ||
698 | if (!mreq.imr_ifindex) { | |
e6f1cebf | 699 | if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) { |
132adf54 SH |
700 | inet->mc_index = 0; |
701 | inet->mc_addr = 0; | |
702 | err = 0; | |
1da177e4 LT |
703 | break; |
704 | } | |
3b1e0a65 | 705 | dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr); |
55b80503 | 706 | if (dev) |
132adf54 | 707 | mreq.imr_ifindex = dev->ifindex; |
132adf54 | 708 | } else |
55b80503 | 709 | dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex); |
1da177e4 | 710 | |
1da177e4 | 711 | |
132adf54 SH |
712 | err = -EADDRNOTAVAIL; |
713 | if (!dev) | |
714 | break; | |
55b80503 | 715 | dev_put(dev); |
132adf54 SH |
716 | |
717 | err = -EINVAL; | |
718 | if (sk->sk_bound_dev_if && | |
719 | mreq.imr_ifindex != sk->sk_bound_dev_if) | |
720 | break; | |
1da177e4 | 721 | |
132adf54 SH |
722 | inet->mc_index = mreq.imr_ifindex; |
723 | inet->mc_addr = mreq.imr_address.s_addr; | |
724 | err = 0; | |
725 | break; | |
726 | } | |
1da177e4 | 727 | |
132adf54 SH |
728 | case IP_ADD_MEMBERSHIP: |
729 | case IP_DROP_MEMBERSHIP: | |
730 | { | |
731 | struct ip_mreqn mreq; | |
1da177e4 | 732 | |
a96fb49b FL |
733 | err = -EPROTO; |
734 | if (inet_sk(sk)->is_icsk) | |
735 | break; | |
736 | ||
132adf54 SH |
737 | if (optlen < sizeof(struct ip_mreq)) |
738 | goto e_inval; | |
739 | err = -EFAULT; | |
740 | if (optlen >= sizeof(struct ip_mreqn)) { | |
09cb105e | 741 | if (copy_from_user(&mreq, optval, sizeof(mreq))) |
1da177e4 | 742 | break; |
132adf54 SH |
743 | } else { |
744 | memset(&mreq, 0, sizeof(mreq)); | |
09cb105e | 745 | if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq))) |
1da177e4 | 746 | break; |
132adf54 | 747 | } |
1da177e4 | 748 | |
132adf54 SH |
749 | if (optname == IP_ADD_MEMBERSHIP) |
750 | err = ip_mc_join_group(sk, &mreq); | |
751 | else | |
752 | err = ip_mc_leave_group(sk, &mreq); | |
753 | break; | |
754 | } | |
755 | case IP_MSFILTER: | |
756 | { | |
132adf54 SH |
757 | struct ip_msfilter *msf; |
758 | ||
759 | if (optlen < IP_MSFILTER_SIZE(0)) | |
760 | goto e_inval; | |
761 | if (optlen > sysctl_optmem_max) { | |
762 | err = -ENOBUFS; | |
1da177e4 LT |
763 | break; |
764 | } | |
132adf54 | 765 | msf = kmalloc(optlen, GFP_KERNEL); |
cfcabdcc | 766 | if (!msf) { |
132adf54 SH |
767 | err = -ENOBUFS; |
768 | break; | |
769 | } | |
770 | err = -EFAULT; | |
771 | if (copy_from_user(msf, optval, optlen)) { | |
772 | kfree(msf); | |
773 | break; | |
774 | } | |
775 | /* numsrc >= (1G-4) overflow in 32 bits */ | |
776 | if (msf->imsf_numsrc >= 0x3ffffffcU || | |
777 | msf->imsf_numsrc > sysctl_igmp_max_msf) { | |
778 | kfree(msf); | |
779 | err = -ENOBUFS; | |
780 | break; | |
781 | } | |
782 | if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) { | |
783 | kfree(msf); | |
784 | err = -EINVAL; | |
785 | break; | |
786 | } | |
787 | err = ip_mc_msfilter(sk, msf, 0); | |
788 | kfree(msf); | |
789 | break; | |
790 | } | |
791 | case IP_BLOCK_SOURCE: | |
792 | case IP_UNBLOCK_SOURCE: | |
793 | case IP_ADD_SOURCE_MEMBERSHIP: | |
794 | case IP_DROP_SOURCE_MEMBERSHIP: | |
795 | { | |
796 | struct ip_mreq_source mreqs; | |
797 | int omode, add; | |
1da177e4 | 798 | |
132adf54 SH |
799 | if (optlen != sizeof(struct ip_mreq_source)) |
800 | goto e_inval; | |
801 | if (copy_from_user(&mreqs, optval, sizeof(mreqs))) { | |
1da177e4 | 802 | err = -EFAULT; |
1da177e4 LT |
803 | break; |
804 | } | |
132adf54 SH |
805 | if (optname == IP_BLOCK_SOURCE) { |
806 | omode = MCAST_EXCLUDE; | |
807 | add = 1; | |
808 | } else if (optname == IP_UNBLOCK_SOURCE) { | |
809 | omode = MCAST_EXCLUDE; | |
810 | add = 0; | |
811 | } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) { | |
812 | struct ip_mreqn mreq; | |
1da177e4 | 813 | |
132adf54 SH |
814 | mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr; |
815 | mreq.imr_address.s_addr = mreqs.imr_interface; | |
816 | mreq.imr_ifindex = 0; | |
817 | err = ip_mc_join_group(sk, &mreq); | |
818 | if (err && err != -EADDRINUSE) | |
1da177e4 | 819 | break; |
132adf54 SH |
820 | omode = MCAST_INCLUDE; |
821 | add = 1; | |
822 | } else /* IP_DROP_SOURCE_MEMBERSHIP */ { | |
823 | omode = MCAST_INCLUDE; | |
824 | add = 0; | |
825 | } | |
826 | err = ip_mc_source(add, omode, sk, &mreqs, 0); | |
827 | break; | |
828 | } | |
829 | case MCAST_JOIN_GROUP: | |
830 | case MCAST_LEAVE_GROUP: | |
831 | { | |
832 | struct group_req greq; | |
833 | struct sockaddr_in *psin; | |
834 | struct ip_mreqn mreq; | |
835 | ||
836 | if (optlen < sizeof(struct group_req)) | |
837 | goto e_inval; | |
838 | err = -EFAULT; | |
839 | if (copy_from_user(&greq, optval, sizeof(greq))) | |
840 | break; | |
841 | psin = (struct sockaddr_in *)&greq.gr_group; | |
842 | if (psin->sin_family != AF_INET) | |
843 | goto e_inval; | |
844 | memset(&mreq, 0, sizeof(mreq)); | |
845 | mreq.imr_multiaddr = psin->sin_addr; | |
846 | mreq.imr_ifindex = greq.gr_interface; | |
847 | ||
848 | if (optname == MCAST_JOIN_GROUP) | |
849 | err = ip_mc_join_group(sk, &mreq); | |
850 | else | |
851 | err = ip_mc_leave_group(sk, &mreq); | |
852 | break; | |
853 | } | |
854 | case MCAST_JOIN_SOURCE_GROUP: | |
855 | case MCAST_LEAVE_SOURCE_GROUP: | |
856 | case MCAST_BLOCK_SOURCE: | |
857 | case MCAST_UNBLOCK_SOURCE: | |
858 | { | |
859 | struct group_source_req greqs; | |
860 | struct ip_mreq_source mreqs; | |
861 | struct sockaddr_in *psin; | |
862 | int omode, add; | |
863 | ||
864 | if (optlen != sizeof(struct group_source_req)) | |
865 | goto e_inval; | |
866 | if (copy_from_user(&greqs, optval, sizeof(greqs))) { | |
1da177e4 | 867 | err = -EFAULT; |
1da177e4 LT |
868 | break; |
869 | } | |
132adf54 SH |
870 | if (greqs.gsr_group.ss_family != AF_INET || |
871 | greqs.gsr_source.ss_family != AF_INET) { | |
872 | err = -EADDRNOTAVAIL; | |
1da177e4 LT |
873 | break; |
874 | } | |
132adf54 SH |
875 | psin = (struct sockaddr_in *)&greqs.gsr_group; |
876 | mreqs.imr_multiaddr = psin->sin_addr.s_addr; | |
877 | psin = (struct sockaddr_in *)&greqs.gsr_source; | |
878 | mreqs.imr_sourceaddr = psin->sin_addr.s_addr; | |
879 | mreqs.imr_interface = 0; /* use index for mc_source */ | |
880 | ||
881 | if (optname == MCAST_BLOCK_SOURCE) { | |
882 | omode = MCAST_EXCLUDE; | |
883 | add = 1; | |
884 | } else if (optname == MCAST_UNBLOCK_SOURCE) { | |
885 | omode = MCAST_EXCLUDE; | |
886 | add = 0; | |
887 | } else if (optname == MCAST_JOIN_SOURCE_GROUP) { | |
1da177e4 LT |
888 | struct ip_mreqn mreq; |
889 | ||
132adf54 | 890 | psin = (struct sockaddr_in *)&greqs.gsr_group; |
1da177e4 | 891 | mreq.imr_multiaddr = psin->sin_addr; |
132adf54 SH |
892 | mreq.imr_address.s_addr = 0; |
893 | mreq.imr_ifindex = greqs.gsr_interface; | |
894 | err = ip_mc_join_group(sk, &mreq); | |
895 | if (err && err != -EADDRINUSE) | |
896 | break; | |
897 | greqs.gsr_interface = mreq.imr_ifindex; | |
898 | omode = MCAST_INCLUDE; | |
899 | add = 1; | |
900 | } else /* MCAST_LEAVE_SOURCE_GROUP */ { | |
901 | omode = MCAST_INCLUDE; | |
902 | add = 0; | |
903 | } | |
904 | err = ip_mc_source(add, omode, sk, &mreqs, | |
905 | greqs.gsr_interface); | |
906 | break; | |
907 | } | |
908 | case MCAST_MSFILTER: | |
909 | { | |
132adf54 SH |
910 | struct sockaddr_in *psin; |
911 | struct ip_msfilter *msf = NULL; | |
912 | struct group_filter *gsf = NULL; | |
913 | int msize, i, ifindex; | |
914 | ||
915 | if (optlen < GROUP_FILTER_SIZE(0)) | |
916 | goto e_inval; | |
917 | if (optlen > sysctl_optmem_max) { | |
918 | err = -ENOBUFS; | |
1da177e4 LT |
919 | break; |
920 | } | |
09cb105e | 921 | gsf = kmalloc(optlen, GFP_KERNEL); |
cfcabdcc | 922 | if (!gsf) { |
132adf54 | 923 | err = -ENOBUFS; |
1da177e4 LT |
924 | break; |
925 | } | |
132adf54 | 926 | err = -EFAULT; |
4d52cfbe | 927 | if (copy_from_user(gsf, optval, optlen)) |
132adf54 | 928 | goto mc_msf_out; |
4d52cfbe | 929 | |
132adf54 SH |
930 | /* numsrc >= (4G-140)/128 overflow in 32 bits */ |
931 | if (gsf->gf_numsrc >= 0x1ffffff || | |
932 | gsf->gf_numsrc > sysctl_igmp_max_msf) { | |
933 | err = -ENOBUFS; | |
934 | goto mc_msf_out; | |
935 | } | |
936 | if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) { | |
937 | err = -EINVAL; | |
938 | goto mc_msf_out; | |
939 | } | |
940 | msize = IP_MSFILTER_SIZE(gsf->gf_numsrc); | |
09cb105e | 941 | msf = kmalloc(msize, GFP_KERNEL); |
cfcabdcc | 942 | if (!msf) { |
132adf54 SH |
943 | err = -ENOBUFS; |
944 | goto mc_msf_out; | |
945 | } | |
946 | ifindex = gsf->gf_interface; | |
947 | psin = (struct sockaddr_in *)&gsf->gf_group; | |
948 | if (psin->sin_family != AF_INET) { | |
1da177e4 | 949 | err = -EADDRNOTAVAIL; |
132adf54 | 950 | goto mc_msf_out; |
1da177e4 | 951 | } |
132adf54 SH |
952 | msf->imsf_multiaddr = psin->sin_addr.s_addr; |
953 | msf->imsf_interface = 0; | |
954 | msf->imsf_fmode = gsf->gf_fmode; | |
955 | msf->imsf_numsrc = gsf->gf_numsrc; | |
956 | err = -EADDRNOTAVAIL; | |
4d52cfbe | 957 | for (i = 0; i < gsf->gf_numsrc; ++i) { |
132adf54 | 958 | psin = (struct sockaddr_in *)&gsf->gf_slist[i]; |
e905a9ed | 959 | |
132adf54 SH |
960 | if (psin->sin_family != AF_INET) |
961 | goto mc_msf_out; | |
962 | msf->imsf_slist[i] = psin->sin_addr.s_addr; | |
963 | } | |
964 | kfree(gsf); | |
965 | gsf = NULL; | |
966 | ||
967 | err = ip_mc_msfilter(sk, msf, ifindex); | |
4d52cfbe | 968 | mc_msf_out: |
132adf54 SH |
969 | kfree(msf); |
970 | kfree(gsf); | |
971 | break; | |
972 | } | |
f771bef9 NS |
973 | case IP_MULTICAST_ALL: |
974 | if (optlen < 1) | |
975 | goto e_inval; | |
976 | if (val != 0 && val != 1) | |
977 | goto e_inval; | |
978 | inet->mc_all = val; | |
979 | break; | |
132adf54 SH |
980 | case IP_ROUTER_ALERT: |
981 | err = ip_ra_control(sk, val ? 1 : 0, NULL); | |
982 | break; | |
983 | ||
984 | case IP_FREEBIND: | |
4d52cfbe | 985 | if (optlen < 1) |
132adf54 SH |
986 | goto e_inval; |
987 | inet->freebind = !!val; | |
988 | break; | |
989 | ||
990 | case IP_IPSEC_POLICY: | |
991 | case IP_XFRM_POLICY: | |
992 | err = -EPERM; | |
52e804c6 | 993 | if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) |
1da177e4 | 994 | break; |
132adf54 SH |
995 | err = xfrm_user_policy(sk, optname, optval, optlen); |
996 | break; | |
1da177e4 | 997 | |
f5715aea | 998 | case IP_TRANSPARENT: |
52e804c6 EB |
999 | if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) && |
1000 | !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) { | |
f5715aea KK |
1001 | err = -EPERM; |
1002 | break; | |
1003 | } | |
1004 | if (optlen < 1) | |
1005 | goto e_inval; | |
1006 | inet->transparent = !!val; | |
1007 | break; | |
1008 | ||
d218d111 SH |
1009 | case IP_MINTTL: |
1010 | if (optlen < 1) | |
1011 | goto e_inval; | |
1012 | if (val < 0 || val > 255) | |
1013 | goto e_inval; | |
1014 | inet->min_ttl = val; | |
1015 | break; | |
1016 | ||
132adf54 SH |
1017 | default: |
1018 | err = -ENOPROTOOPT; | |
1019 | break; | |
1da177e4 LT |
1020 | } |
1021 | release_sock(sk); | |
1022 | return err; | |
1023 | ||
1024 | e_inval: | |
1025 | release_sock(sk); | |
1026 | return -EINVAL; | |
1027 | } | |
1028 | ||
f84af32c | 1029 | /** |
d826eb14 | 1030 | * ipv4_pktinfo_prepare - transfert some info from rtable to skb |
f84af32c ED |
1031 | * @sk: socket |
1032 | * @skb: buffer | |
1033 | * | |
35ebf65e DM |
1034 | * To support IP_CMSG_PKTINFO option, we store rt_iif and specific |
1035 | * destination in skb->cb[] before dst drop. | |
d826eb14 | 1036 | * This way, receiver doesnt make cache line misses to read rtable. |
f84af32c | 1037 | */ |
d826eb14 | 1038 | void ipv4_pktinfo_prepare(struct sk_buff *skb) |
f84af32c | 1039 | { |
d826eb14 | 1040 | struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb); |
d826eb14 | 1041 | |
92101b3b DM |
1042 | if (skb_rtable(skb)) { |
1043 | pktinfo->ipi_ifindex = inet_iif(skb); | |
35ebf65e | 1044 | pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb); |
d826eb14 ED |
1045 | } else { |
1046 | pktinfo->ipi_ifindex = 0; | |
1047 | pktinfo->ipi_spec_dst.s_addr = 0; | |
1048 | } | |
4b9e9796 S |
1049 | /* We need to keep the dst for __ip_options_echo() |
1050 | * We could restrict the test to opt.ts_needtime || opt.srr, | |
1051 | * but the following is good enough as IP options are not often used. | |
1052 | */ | |
1053 | if (unlikely(IPCB(skb)->opt.optlen)) | |
1054 | skb_dst_force(skb); | |
1055 | else | |
d826eb14 | 1056 | skb_dst_drop(skb); |
f84af32c | 1057 | } |
f84af32c | 1058 | |
3fdadf7d | 1059 | int ip_setsockopt(struct sock *sk, int level, |
b7058842 | 1060 | int optname, char __user *optval, unsigned int optlen) |
3fdadf7d DM |
1061 | { |
1062 | int err; | |
1063 | ||
1064 | if (level != SOL_IP) | |
1065 | return -ENOPROTOOPT; | |
1066 | ||
1067 | err = do_ip_setsockopt(sk, level, optname, optval, optlen); | |
1068 | #ifdef CONFIG_NETFILTER | |
1069 | /* we need to exclude all possible ENOPROTOOPTs except default case */ | |
1070 | if (err == -ENOPROTOOPT && optname != IP_HDRINCL && | |
6a9fb947 PE |
1071 | optname != IP_IPSEC_POLICY && |
1072 | optname != IP_XFRM_POLICY && | |
1073 | !ip_mroute_opt(optname)) { | |
3fdadf7d DM |
1074 | lock_sock(sk); |
1075 | err = nf_setsockopt(sk, PF_INET, optname, optval, optlen); | |
1076 | release_sock(sk); | |
1077 | } | |
1078 | #endif | |
1079 | return err; | |
1080 | } | |
4d52cfbe | 1081 | EXPORT_SYMBOL(ip_setsockopt); |
3fdadf7d DM |
1082 | |
1083 | #ifdef CONFIG_COMPAT | |
543d9cfe | 1084 | int compat_ip_setsockopt(struct sock *sk, int level, int optname, |
b7058842 | 1085 | char __user *optval, unsigned int optlen) |
3fdadf7d DM |
1086 | { |
1087 | int err; | |
1088 | ||
1089 | if (level != SOL_IP) | |
1090 | return -ENOPROTOOPT; | |
1091 | ||
dae50295 DS |
1092 | if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER) |
1093 | return compat_mc_setsockopt(sk, level, optname, optval, optlen, | |
1094 | ip_setsockopt); | |
1095 | ||
3fdadf7d DM |
1096 | err = do_ip_setsockopt(sk, level, optname, optval, optlen); |
1097 | #ifdef CONFIG_NETFILTER | |
1098 | /* we need to exclude all possible ENOPROTOOPTs except default case */ | |
1099 | if (err == -ENOPROTOOPT && optname != IP_HDRINCL && | |
6a9fb947 PE |
1100 | optname != IP_IPSEC_POLICY && |
1101 | optname != IP_XFRM_POLICY && | |
1102 | !ip_mroute_opt(optname)) { | |
3fdadf7d | 1103 | lock_sock(sk); |
543d9cfe ACM |
1104 | err = compat_nf_setsockopt(sk, PF_INET, optname, |
1105 | optval, optlen); | |
3fdadf7d DM |
1106 | release_sock(sk); |
1107 | } | |
1108 | #endif | |
1109 | return err; | |
1110 | } | |
543d9cfe | 1111 | EXPORT_SYMBOL(compat_ip_setsockopt); |
3fdadf7d DM |
1112 | #endif |
1113 | ||
1da177e4 | 1114 | /* |
4d52cfbe ED |
1115 | * Get the options. Note for future reference. The GET of IP options gets |
1116 | * the _received_ ones. The set sets the _sent_ ones. | |
1da177e4 LT |
1117 | */ |
1118 | ||
3fdadf7d | 1119 | static int do_ip_getsockopt(struct sock *sk, int level, int optname, |
95c96174 | 1120 | char __user *optval, int __user *optlen, unsigned int flags) |
1da177e4 LT |
1121 | { |
1122 | struct inet_sock *inet = inet_sk(sk); | |
1123 | int val; | |
1124 | int len; | |
e905a9ed | 1125 | |
132adf54 | 1126 | if (level != SOL_IP) |
1da177e4 LT |
1127 | return -EOPNOTSUPP; |
1128 | ||
6a9fb947 | 1129 | if (ip_mroute_opt(optname)) |
09cb105e | 1130 | return ip_mroute_getsockopt(sk, optname, optval, optlen); |
1da177e4 | 1131 | |
09cb105e | 1132 | if (get_user(len, optlen)) |
1da177e4 | 1133 | return -EFAULT; |
132adf54 | 1134 | if (len < 0) |
1da177e4 | 1135 | return -EINVAL; |
e905a9ed | 1136 | |
1da177e4 LT |
1137 | lock_sock(sk); |
1138 | ||
132adf54 SH |
1139 | switch (optname) { |
1140 | case IP_OPTIONS: | |
1141 | { | |
1142 | unsigned char optbuf[sizeof(struct ip_options)+40]; | |
f6d8bd05 ED |
1143 | struct ip_options *opt = (struct ip_options *)optbuf; |
1144 | struct ip_options_rcu *inet_opt; | |
1145 | ||
1146 | inet_opt = rcu_dereference_protected(inet->inet_opt, | |
1147 | sock_owned_by_user(sk)); | |
132adf54 | 1148 | opt->optlen = 0; |
f6d8bd05 ED |
1149 | if (inet_opt) |
1150 | memcpy(optbuf, &inet_opt->opt, | |
1151 | sizeof(struct ip_options) + | |
1152 | inet_opt->opt.optlen); | |
132adf54 SH |
1153 | release_sock(sk); |
1154 | ||
1155 | if (opt->optlen == 0) | |
1156 | return put_user(0, optlen); | |
1157 | ||
1158 | ip_options_undo(opt); | |
1159 | ||
1160 | len = min_t(unsigned int, len, opt->optlen); | |
1161 | if (put_user(len, optlen)) | |
1162 | return -EFAULT; | |
1163 | if (copy_to_user(optval, opt->__data, len)) | |
1164 | return -EFAULT; | |
1165 | return 0; | |
1166 | } | |
1167 | case IP_PKTINFO: | |
1168 | val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0; | |
1169 | break; | |
1170 | case IP_RECVTTL: | |
1171 | val = (inet->cmsg_flags & IP_CMSG_TTL) != 0; | |
1172 | break; | |
1173 | case IP_RECVTOS: | |
1174 | val = (inet->cmsg_flags & IP_CMSG_TOS) != 0; | |
1175 | break; | |
1176 | case IP_RECVOPTS: | |
1177 | val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0; | |
1178 | break; | |
1179 | case IP_RETOPTS: | |
1180 | val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0; | |
1181 | break; | |
1182 | case IP_PASSSEC: | |
1183 | val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0; | |
1184 | break; | |
e8b2dfe9 BS |
1185 | case IP_RECVORIGDSTADDR: |
1186 | val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0; | |
1187 | break; | |
132adf54 SH |
1188 | case IP_TOS: |
1189 | val = inet->tos; | |
1190 | break; | |
1191 | case IP_TTL: | |
1192 | val = (inet->uc_ttl == -1 ? | |
1193 | sysctl_ip_default_ttl : | |
1194 | inet->uc_ttl); | |
1195 | break; | |
1196 | case IP_HDRINCL: | |
1197 | val = inet->hdrincl; | |
1198 | break; | |
a89b4763 MK |
1199 | case IP_NODEFRAG: |
1200 | val = inet->nodefrag; | |
1201 | break; | |
132adf54 SH |
1202 | case IP_MTU_DISCOVER: |
1203 | val = inet->pmtudisc; | |
1204 | break; | |
1205 | case IP_MTU: | |
1206 | { | |
1207 | struct dst_entry *dst; | |
1208 | val = 0; | |
1209 | dst = sk_dst_get(sk); | |
1210 | if (dst) { | |
1211 | val = dst_mtu(dst); | |
1212 | dst_release(dst); | |
1da177e4 | 1213 | } |
132adf54 | 1214 | if (!val) { |
1da177e4 | 1215 | release_sock(sk); |
132adf54 | 1216 | return -ENOTCONN; |
1da177e4 | 1217 | } |
132adf54 SH |
1218 | break; |
1219 | } | |
1220 | case IP_RECVERR: | |
1221 | val = inet->recverr; | |
1222 | break; | |
1223 | case IP_MULTICAST_TTL: | |
1224 | val = inet->mc_ttl; | |
1225 | break; | |
1226 | case IP_MULTICAST_LOOP: | |
1227 | val = inet->mc_loop; | |
1228 | break; | |
76e21053 EH |
1229 | case IP_UNICAST_IF: |
1230 | val = (__force int)htonl((__u32) inet->uc_index); | |
1231 | break; | |
132adf54 SH |
1232 | case IP_MULTICAST_IF: |
1233 | { | |
1234 | struct in_addr addr; | |
1235 | len = min_t(unsigned int, len, sizeof(struct in_addr)); | |
1236 | addr.s_addr = inet->mc_addr; | |
1237 | release_sock(sk); | |
1da177e4 | 1238 | |
132adf54 SH |
1239 | if (put_user(len, optlen)) |
1240 | return -EFAULT; | |
1241 | if (copy_to_user(optval, &addr, len)) | |
1242 | return -EFAULT; | |
1243 | return 0; | |
1244 | } | |
1245 | case IP_MSFILTER: | |
1246 | { | |
1247 | struct ip_msfilter msf; | |
1248 | int err; | |
1249 | ||
1250 | if (len < IP_MSFILTER_SIZE(0)) { | |
1da177e4 | 1251 | release_sock(sk); |
132adf54 | 1252 | return -EINVAL; |
1da177e4 | 1253 | } |
132adf54 | 1254 | if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) { |
1da177e4 | 1255 | release_sock(sk); |
132adf54 | 1256 | return -EFAULT; |
1da177e4 | 1257 | } |
132adf54 SH |
1258 | err = ip_mc_msfget(sk, &msf, |
1259 | (struct ip_msfilter __user *)optval, optlen); | |
1260 | release_sock(sk); | |
1261 | return err; | |
1262 | } | |
1263 | case MCAST_MSFILTER: | |
1264 | { | |
1265 | struct group_filter gsf; | |
1266 | int err; | |
1da177e4 | 1267 | |
132adf54 | 1268 | if (len < GROUP_FILTER_SIZE(0)) { |
1da177e4 | 1269 | release_sock(sk); |
132adf54 SH |
1270 | return -EINVAL; |
1271 | } | |
1272 | if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) { | |
1273 | release_sock(sk); | |
1274 | return -EFAULT; | |
1275 | } | |
1276 | err = ip_mc_gsfget(sk, &gsf, | |
4d52cfbe ED |
1277 | (struct group_filter __user *)optval, |
1278 | optlen); | |
132adf54 SH |
1279 | release_sock(sk); |
1280 | return err; | |
1281 | } | |
f771bef9 NS |
1282 | case IP_MULTICAST_ALL: |
1283 | val = inet->mc_all; | |
1284 | break; | |
132adf54 SH |
1285 | case IP_PKTOPTIONS: |
1286 | { | |
1287 | struct msghdr msg; | |
1da177e4 | 1288 | |
132adf54 | 1289 | release_sock(sk); |
1da177e4 | 1290 | |
132adf54 SH |
1291 | if (sk->sk_type != SOCK_STREAM) |
1292 | return -ENOPROTOOPT; | |
1da177e4 | 1293 | |
132adf54 SH |
1294 | msg.msg_control = optval; |
1295 | msg.msg_controllen = len; | |
dd23198e | 1296 | msg.msg_flags = flags; |
1da177e4 | 1297 | |
132adf54 SH |
1298 | if (inet->cmsg_flags & IP_CMSG_PKTINFO) { |
1299 | struct in_pktinfo info; | |
1300 | ||
c720c7e8 ED |
1301 | info.ipi_addr.s_addr = inet->inet_rcv_saddr; |
1302 | info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr; | |
132adf54 SH |
1303 | info.ipi_ifindex = inet->mc_index; |
1304 | put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info); | |
1da177e4 | 1305 | } |
132adf54 SH |
1306 | if (inet->cmsg_flags & IP_CMSG_TTL) { |
1307 | int hlim = inet->mc_ttl; | |
1308 | put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim); | |
1309 | } | |
4c507d28 JB |
1310 | if (inet->cmsg_flags & IP_CMSG_TOS) { |
1311 | int tos = inet->rcv_tos; | |
1312 | put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos); | |
1313 | } | |
132adf54 SH |
1314 | len -= msg.msg_controllen; |
1315 | return put_user(len, optlen); | |
1316 | } | |
1317 | case IP_FREEBIND: | |
1318 | val = inet->freebind; | |
1319 | break; | |
f5715aea KK |
1320 | case IP_TRANSPARENT: |
1321 | val = inet->transparent; | |
1322 | break; | |
d218d111 SH |
1323 | case IP_MINTTL: |
1324 | val = inet->min_ttl; | |
1325 | break; | |
132adf54 SH |
1326 | default: |
1327 | release_sock(sk); | |
1328 | return -ENOPROTOOPT; | |
1da177e4 LT |
1329 | } |
1330 | release_sock(sk); | |
e905a9ed | 1331 | |
4d52cfbe | 1332 | if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) { |
1da177e4 LT |
1333 | unsigned char ucval = (unsigned char)val; |
1334 | len = 1; | |
132adf54 | 1335 | if (put_user(len, optlen)) |
1da177e4 | 1336 | return -EFAULT; |
09cb105e | 1337 | if (copy_to_user(optval, &ucval, 1)) |
1da177e4 LT |
1338 | return -EFAULT; |
1339 | } else { | |
1340 | len = min_t(unsigned int, sizeof(int), len); | |
132adf54 | 1341 | if (put_user(len, optlen)) |
1da177e4 | 1342 | return -EFAULT; |
09cb105e | 1343 | if (copy_to_user(optval, &val, len)) |
1da177e4 LT |
1344 | return -EFAULT; |
1345 | } | |
1346 | return 0; | |
1347 | } | |
1348 | ||
3fdadf7d | 1349 | int ip_getsockopt(struct sock *sk, int level, |
132adf54 | 1350 | int optname, char __user *optval, int __user *optlen) |
3fdadf7d DM |
1351 | { |
1352 | int err; | |
1353 | ||
dd23198e | 1354 | err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0); |
3fdadf7d DM |
1355 | #ifdef CONFIG_NETFILTER |
1356 | /* we need to exclude all possible ENOPROTOOPTs except default case */ | |
6a9fb947 PE |
1357 | if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS && |
1358 | !ip_mroute_opt(optname)) { | |
e905a9ed | 1359 | int len; |
3fdadf7d | 1360 | |
09cb105e | 1361 | if (get_user(len, optlen)) |
3fdadf7d DM |
1362 | return -EFAULT; |
1363 | ||
1364 | lock_sock(sk); | |
1365 | err = nf_getsockopt(sk, PF_INET, optname, optval, | |
1366 | &len); | |
1367 | release_sock(sk); | |
1368 | if (err >= 0) | |
1369 | err = put_user(len, optlen); | |
1370 | return err; | |
1371 | } | |
1372 | #endif | |
1373 | return err; | |
1374 | } | |
4d52cfbe | 1375 | EXPORT_SYMBOL(ip_getsockopt); |
3fdadf7d DM |
1376 | |
1377 | #ifdef CONFIG_COMPAT | |
543d9cfe ACM |
1378 | int compat_ip_getsockopt(struct sock *sk, int level, int optname, |
1379 | char __user *optval, int __user *optlen) | |
3fdadf7d | 1380 | { |
42908c69 DS |
1381 | int err; |
1382 | ||
1383 | if (optname == MCAST_MSFILTER) | |
1384 | return compat_mc_getsockopt(sk, level, optname, optval, optlen, | |
1385 | ip_getsockopt); | |
1386 | ||
dd23198e DB |
1387 | err = do_ip_getsockopt(sk, level, optname, optval, optlen, |
1388 | MSG_CMSG_COMPAT); | |
42908c69 | 1389 | |
3fdadf7d DM |
1390 | #ifdef CONFIG_NETFILTER |
1391 | /* we need to exclude all possible ENOPROTOOPTs except default case */ | |
6a9fb947 PE |
1392 | if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS && |
1393 | !ip_mroute_opt(optname)) { | |
e905a9ed | 1394 | int len; |
3fdadf7d | 1395 | |
543d9cfe | 1396 | if (get_user(len, optlen)) |
3fdadf7d DM |
1397 | return -EFAULT; |
1398 | ||
1399 | lock_sock(sk); | |
543d9cfe | 1400 | err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len); |
3fdadf7d DM |
1401 | release_sock(sk); |
1402 | if (err >= 0) | |
1403 | err = put_user(len, optlen); | |
1404 | return err; | |
1405 | } | |
1406 | #endif | |
1407 | return err; | |
1408 | } | |
543d9cfe | 1409 | EXPORT_SYMBOL(compat_ip_getsockopt); |
3fdadf7d | 1410 | #endif |