Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * INET An implementation of the TCP/IP protocol suite for the LINUX |
| 4 | * operating system. INET is implemented using the BSD Socket |
| 5 | * interface as the means of communication with the user level. |
| 6 | * |
| 7 | * RAW - implementation of IP "raw" sockets. |
| 8 | * |
Jesper Juhl | 02c30a8 | 2005-05-05 16:16:16 -0700 | [diff] [blame] | 9 | * Authors: Ross Biro |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> |
| 11 | * |
| 12 | * Fixes: |
| 13 | * Alan Cox : verify_area() fixed up |
| 14 | * Alan Cox : ICMP error handling |
| 15 | * Alan Cox : EMSGSIZE if you send too big a packet |
| 16 | * Alan Cox : Now uses generic datagrams and shared |
| 17 | * skbuff library. No more peek crashes, |
| 18 | * no more backlogs |
| 19 | * Alan Cox : Checks sk->broadcast. |
| 20 | * Alan Cox : Uses skb_free_datagram/skb_copy_datagram |
| 21 | * Alan Cox : Raw passes ip options too |
| 22 | * Alan Cox : Setsocketopt added |
| 23 | * Alan Cox : Fixed error return for broadcasts |
| 24 | * Alan Cox : Removed wake_up calls |
| 25 | * Alan Cox : Use ttl/tos |
| 26 | * Alan Cox : Cleaned up old debugging |
| 27 | * Alan Cox : Use new kernel side addresses |
| 28 | * Arnt Gulbrandsen : Fixed MSG_DONTROUTE in raw sockets. |
| 29 | * Alan Cox : BSD style RAW socket demultiplexing. |
| 30 | * Alan Cox : Beginnings of mrouted support. |
| 31 | * Alan Cox : Added IP_HDRINCL option. |
| 32 | * Alan Cox : Skip broadcast check if BSDism set. |
| 33 | * David S. Miller : New socket lookup architecture. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | */ |
Dave Jones | bf0d524 | 2006-09-22 14:00:29 -0700 | [diff] [blame] | 35 | |
Alan Cox | 715b49e | 2006-01-18 17:44:07 -0800 | [diff] [blame] | 36 | #include <linux/types.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 37 | #include <linux/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <asm/byteorder.h> |
| 39 | #include <asm/current.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 40 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <asm/ioctls.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <linux/stddef.h> |
| 43 | #include <linux/slab.h> |
| 44 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/kernel.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 46 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <linux/spinlock.h> |
| 48 | #include <linux/sockios.h> |
| 49 | #include <linux/socket.h> |
| 50 | #include <linux/in.h> |
| 51 | #include <linux/mroute.h> |
| 52 | #include <linux/netdevice.h> |
| 53 | #include <linux/in_route.h> |
| 54 | #include <linux/route.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #include <linux/skbuff.h> |
Quentin Armitage | f5220d6 | 2014-07-23 09:58:01 +0100 | [diff] [blame] | 56 | #include <linux/igmp.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 57 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | #include <net/dst.h> |
| 59 | #include <net/sock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #include <linux/ip.h> |
| 61 | #include <linux/net.h> |
| 62 | #include <net/ip.h> |
| 63 | #include <net/icmp.h> |
| 64 | #include <net/udp.h> |
| 65 | #include <net/raw.h> |
| 66 | #include <net/snmp.h> |
Arnaldo Carvalho de Melo | c752f07 | 2005-08-09 20:08:28 -0700 | [diff] [blame] | 67 | #include <net/tcp_states.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | #include <net/inet_common.h> |
| 69 | #include <net/checksum.h> |
| 70 | #include <net/xfrm.h> |
| 71 | #include <linux/rtnetlink.h> |
| 72 | #include <linux/proc_fs.h> |
| 73 | #include <linux/seq_file.h> |
| 74 | #include <linux/netfilter.h> |
| 75 | #include <linux/netfilter_ipv4.h> |
Eric W. Biederman | 709b46e | 2011-01-29 16:15:56 +0000 | [diff] [blame] | 76 | #include <linux/compat.h> |
Herbert Xu | c008ba5b | 2014-11-07 21:27:09 +0800 | [diff] [blame] | 77 | #include <linux/uio.h> |
| 78 | |
| 79 | struct raw_frag_vec { |
Al Viro | b61e9dc | 2014-11-24 10:52:29 -0500 | [diff] [blame] | 80 | struct msghdr *msg; |
Herbert Xu | c008ba5b | 2014-11-07 21:27:09 +0800 | [diff] [blame] | 81 | union { |
| 82 | struct icmphdr icmph; |
| 83 | char c[1]; |
| 84 | } hdr; |
| 85 | int hlen; |
| 86 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
Cyrill Gorcunov | 432490f | 2016-10-21 13:03:44 +0300 | [diff] [blame] | 88 | struct raw_hashinfo raw_v4_hashinfo = { |
Robert P. J. Day | 938b93a | 2008-03-18 00:59:23 -0700 | [diff] [blame] | 89 | .lock = __RW_LOCK_UNLOCKED(raw_v4_hashinfo.lock), |
Pavel Emelyanov | b673e4d | 2007-11-19 22:36:45 -0800 | [diff] [blame] | 90 | }; |
Cyrill Gorcunov | 432490f | 2016-10-21 13:03:44 +0300 | [diff] [blame] | 91 | EXPORT_SYMBOL_GPL(raw_v4_hashinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
Craig Gallek | 086c653 | 2016-02-10 11:50:35 -0500 | [diff] [blame] | 93 | int raw_hash_sk(struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | { |
Pavel Emelyanov | fc8717b | 2008-03-22 16:56:51 -0700 | [diff] [blame] | 95 | struct raw_hashinfo *h = sk->sk_prot->h.raw_hash; |
Pavel Emelyanov | 65b4c50 | 2007-11-19 22:37:24 -0800 | [diff] [blame] | 96 | struct hlist_head *head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 98 | head = &h->ht[inet_sk(sk)->inet_num & (RAW_HTABLE_SIZE - 1)]; |
Pavel Emelyanov | 65b4c50 | 2007-11-19 22:37:24 -0800 | [diff] [blame] | 99 | |
| 100 | write_lock_bh(&h->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | sk_add_node(sk, head); |
Pavel Emelyanov | 65b4c50 | 2007-11-19 22:37:24 -0800 | [diff] [blame] | 102 | write_unlock_bh(&h->lock); |
Eric Dumazet | b3cb764 | 2021-11-15 09:11:50 -0800 | [diff] [blame] | 103 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); |
Craig Gallek | 086c653 | 2016-02-10 11:50:35 -0500 | [diff] [blame] | 104 | |
| 105 | return 0; |
Pavel Emelyanov | 65b4c50 | 2007-11-19 22:37:24 -0800 | [diff] [blame] | 106 | } |
| 107 | EXPORT_SYMBOL_GPL(raw_hash_sk); |
| 108 | |
Pavel Emelyanov | fc8717b | 2008-03-22 16:56:51 -0700 | [diff] [blame] | 109 | void raw_unhash_sk(struct sock *sk) |
Pavel Emelyanov | ab70768 | 2007-11-19 22:37:58 -0800 | [diff] [blame] | 110 | { |
Pavel Emelyanov | fc8717b | 2008-03-22 16:56:51 -0700 | [diff] [blame] | 111 | struct raw_hashinfo *h = sk->sk_prot->h.raw_hash; |
| 112 | |
Pavel Emelyanov | ab70768 | 2007-11-19 22:37:58 -0800 | [diff] [blame] | 113 | write_lock_bh(&h->lock); |
| 114 | if (sk_del_node_init(sk)) |
Pavel Emelyanov | c29a0bc | 2008-03-31 19:41:46 -0700 | [diff] [blame] | 115 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); |
Pavel Emelyanov | ab70768 | 2007-11-19 22:37:58 -0800 | [diff] [blame] | 116 | write_unlock_bh(&h->lock); |
| 117 | } |
| 118 | EXPORT_SYMBOL_GPL(raw_unhash_sk); |
| 119 | |
Cyrill Gorcunov | 432490f | 2016-10-21 13:03:44 +0300 | [diff] [blame] | 120 | struct sock *__raw_v4_lookup(struct net *net, struct sock *sk, |
David Ahern | 6735993 | 2017-08-07 08:44:18 -0700 | [diff] [blame] | 121 | unsigned short num, __be32 raddr, __be32 laddr, |
| 122 | int dif, int sdif) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 124 | sk_for_each_from(sk) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | struct inet_sock *inet = inet_sk(sk); |
| 126 | |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 127 | if (net_eq(sock_net(sk), net) && inet->inet_num == num && |
| 128 | !(inet->inet_daddr && inet->inet_daddr != raddr) && |
| 129 | !(inet->inet_rcv_saddr && inet->inet_rcv_saddr != laddr) && |
Duncan Eastoe | 7055420 | 2018-11-07 15:36:06 +0000 | [diff] [blame] | 130 | raw_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | goto found; /* gotcha */ |
| 132 | } |
| 133 | sk = NULL; |
| 134 | found: |
| 135 | return sk; |
| 136 | } |
Cyrill Gorcunov | 432490f | 2016-10-21 13:03:44 +0300 | [diff] [blame] | 137 | EXPORT_SYMBOL_GPL(__raw_v4_lookup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
| 139 | /* |
| 140 | * 0 - deliver |
| 141 | * 1 - block |
| 142 | */ |
Eric Dumazet | ab43ed8 | 2012-09-22 00:08:29 +0000 | [diff] [blame] | 143 | static int icmp_filter(const struct sock *sk, const struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { |
Eric Dumazet | ab43ed8 | 2012-09-22 00:08:29 +0000 | [diff] [blame] | 145 | struct icmphdr _hdr; |
| 146 | const struct icmphdr *hdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
Eric Dumazet | ab43ed8 | 2012-09-22 00:08:29 +0000 | [diff] [blame] | 148 | hdr = skb_header_pointer(skb, skb_transport_offset(skb), |
| 149 | sizeof(_hdr), &_hdr); |
| 150 | if (!hdr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | return 1; |
| 152 | |
Eric Dumazet | ab43ed8 | 2012-09-22 00:08:29 +0000 | [diff] [blame] | 153 | if (hdr->type < 32) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | __u32 data = raw_sk(sk)->filter.data; |
| 155 | |
Eric Dumazet | ab43ed8 | 2012-09-22 00:08:29 +0000 | [diff] [blame] | 156 | return ((1U << hdr->type) & data) != 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /* Do not block unknown ICMP types */ |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | /* IP input processing comes here for RAW socket delivery. |
| 164 | * Caller owns SKB, so we must make clones. |
| 165 | * |
| 166 | * RFC 1122: SHOULD pass TOS value up to the transport layer. |
| 167 | * -> It does. And not only TOS, but all IP header. |
| 168 | */ |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 169 | static int raw_v4_input(struct sk_buff *skb, const struct iphdr *iph, int hash) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | { |
David Ahern | 6735993 | 2017-08-07 08:44:18 -0700 | [diff] [blame] | 171 | int sdif = inet_sdif(skb); |
David Ahern | 19e4e76 | 2019-05-07 20:44:59 -0700 | [diff] [blame] | 172 | int dif = inet_iif(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | struct sock *sk; |
| 174 | struct hlist_head *head; |
Patrick McHardy | d13964f | 2005-08-09 19:45:02 -0700 | [diff] [blame] | 175 | int delivered = 0; |
Pavel Emelyanov | be185884 | 2008-01-14 05:35:31 -0800 | [diff] [blame] | 176 | struct net *net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
Pavel Emelyanov | b673e4d | 2007-11-19 22:36:45 -0800 | [diff] [blame] | 178 | read_lock(&raw_v4_hashinfo.lock); |
| 179 | head = &raw_v4_hashinfo.ht[hash]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | if (hlist_empty(head)) |
| 181 | goto out; |
Pavel Emelyanov | be185884 | 2008-01-14 05:35:31 -0800 | [diff] [blame] | 182 | |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 183 | net = dev_net(skb->dev); |
Pavel Emelyanov | be185884 | 2008-01-14 05:35:31 -0800 | [diff] [blame] | 184 | sk = __raw_v4_lookup(net, __sk_head(head), iph->protocol, |
David Ahern | 19e4e76 | 2019-05-07 20:44:59 -0700 | [diff] [blame] | 185 | iph->saddr, iph->daddr, dif, sdif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
| 187 | while (sk) { |
Patrick McHardy | d13964f | 2005-08-09 19:45:02 -0700 | [diff] [blame] | 188 | delivered = 1; |
Quentin Armitage | f5220d6 | 2014-07-23 09:58:01 +0100 | [diff] [blame] | 189 | if ((iph->protocol != IPPROTO_ICMP || !icmp_filter(sk, skb)) && |
| 190 | ip_mc_sf_allow(sk, iph->daddr, iph->saddr, |
David Ahern | 60d9b03 | 2017-08-07 08:44:19 -0700 | [diff] [blame] | 191 | skb->dev->ifindex, sdif)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC); |
| 193 | |
| 194 | /* Not releasing hash table! */ |
| 195 | if (clone) |
| 196 | raw_rcv(sk, clone); |
| 197 | } |
Pavel Emelyanov | be185884 | 2008-01-14 05:35:31 -0800 | [diff] [blame] | 198 | sk = __raw_v4_lookup(net, sk_next(sk), iph->protocol, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | iph->saddr, iph->daddr, |
Stephen Suryaputra | 38c7352 | 2019-06-24 20:14:06 -0400 | [diff] [blame] | 200 | dif, sdif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | } |
| 202 | out: |
Pavel Emelyanov | b673e4d | 2007-11-19 22:36:45 -0800 | [diff] [blame] | 203 | read_unlock(&raw_v4_hashinfo.lock); |
Patrick McHardy | d13964f | 2005-08-09 19:45:02 -0700 | [diff] [blame] | 204 | return delivered; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Pavel Emelyanov | 7bc54c9 | 2007-11-19 22:35:07 -0800 | [diff] [blame] | 207 | int raw_local_deliver(struct sk_buff *skb, int protocol) |
| 208 | { |
| 209 | int hash; |
| 210 | struct sock *raw_sk; |
| 211 | |
Pavel Emelyanov | b673e4d | 2007-11-19 22:36:45 -0800 | [diff] [blame] | 212 | hash = protocol & (RAW_HTABLE_SIZE - 1); |
| 213 | raw_sk = sk_head(&raw_v4_hashinfo.ht[hash]); |
Pavel Emelyanov | 7bc54c9 | 2007-11-19 22:35:07 -0800 | [diff] [blame] | 214 | |
| 215 | /* If there maybe a raw socket we must check - if not we |
| 216 | * don't care less |
| 217 | */ |
| 218 | if (raw_sk && !raw_v4_input(skb, ip_hdr(skb), hash)) |
| 219 | raw_sk = NULL; |
| 220 | |
| 221 | return raw_sk != NULL; |
| 222 | |
| 223 | } |
| 224 | |
| 225 | static void raw_err(struct sock *sk, struct sk_buff *skb, u32 info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | { |
| 227 | struct inet_sock *inet = inet_sk(sk); |
Arnaldo Carvalho de Melo | 88c7664 | 2007-03-13 14:43:18 -0300 | [diff] [blame] | 228 | const int type = icmp_hdr(skb)->type; |
| 229 | const int code = icmp_hdr(skb)->code; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | int err = 0; |
| 231 | int harderr = 0; |
| 232 | |
David S. Miller | 3639339 | 2012-06-14 22:21:46 -0700 | [diff] [blame] | 233 | if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) |
| 234 | ipv4_sk_update_pmtu(skb, sk, info); |
Duan Jiong | 8d65b11 | 2013-09-20 18:21:25 +0800 | [diff] [blame] | 235 | else if (type == ICMP_REDIRECT) { |
David S. Miller | 55be7a9 | 2012-07-11 21:27:49 -0700 | [diff] [blame] | 236 | ipv4_sk_redirect(skb, sk); |
Duan Jiong | 8d65b11 | 2013-09-20 18:21:25 +0800 | [diff] [blame] | 237 | return; |
| 238 | } |
David S. Miller | 3639339 | 2012-06-14 22:21:46 -0700 | [diff] [blame] | 239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | /* Report error on raw socket, if: |
| 241 | 1. User requested ip_recverr. |
| 242 | 2. Socket is connected (otherwise the error indication |
| 243 | is useless without ip_recverr and error is hard. |
| 244 | */ |
| 245 | if (!inet->recverr && sk->sk_state != TCP_ESTABLISHED) |
| 246 | return; |
| 247 | |
| 248 | switch (type) { |
| 249 | default: |
| 250 | case ICMP_TIME_EXCEEDED: |
| 251 | err = EHOSTUNREACH; |
| 252 | break; |
| 253 | case ICMP_SOURCE_QUENCH: |
| 254 | return; |
| 255 | case ICMP_PARAMETERPROB: |
| 256 | err = EPROTO; |
| 257 | harderr = 1; |
| 258 | break; |
| 259 | case ICMP_DEST_UNREACH: |
| 260 | err = EHOSTUNREACH; |
| 261 | if (code > NR_ICMP_UNREACH) |
| 262 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | if (code == ICMP_FRAG_NEEDED) { |
| 264 | harderr = inet->pmtudisc != IP_PMTUDISC_DONT; |
| 265 | err = EMSGSIZE; |
Miaohe Lin | 7551144 | 2020-08-24 07:15:04 -0400 | [diff] [blame] | 266 | } else { |
| 267 | err = icmp_err_convert[code].errno; |
| 268 | harderr = icmp_err_convert[code].fatal; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
| 272 | if (inet->recverr) { |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 273 | const struct iphdr *iph = (const struct iphdr *)skb->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | u8 *payload = skb->data + (iph->ihl << 2); |
| 275 | |
| 276 | if (inet->hdrincl) |
| 277 | payload = skb->data; |
| 278 | ip_icmp_error(sk, skb, err, 0, info, payload); |
| 279 | } |
| 280 | |
| 281 | if (inet->recverr || harderr) { |
| 282 | sk->sk_err = err; |
Alexander Aring | e3ae236 | 2021-06-27 18:48:21 -0400 | [diff] [blame] | 283 | sk_error_report(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
Pavel Emelyanov | 7bc54c9 | 2007-11-19 22:35:07 -0800 | [diff] [blame] | 287 | void raw_icmp_error(struct sk_buff *skb, int protocol, u32 info) |
| 288 | { |
| 289 | int hash; |
| 290 | struct sock *raw_sk; |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 291 | const struct iphdr *iph; |
Pavel Emelyanov | be185884 | 2008-01-14 05:35:31 -0800 | [diff] [blame] | 292 | struct net *net; |
Pavel Emelyanov | 7bc54c9 | 2007-11-19 22:35:07 -0800 | [diff] [blame] | 293 | |
Pavel Emelyanov | b673e4d | 2007-11-19 22:36:45 -0800 | [diff] [blame] | 294 | hash = protocol & (RAW_HTABLE_SIZE - 1); |
Pavel Emelyanov | 7bc54c9 | 2007-11-19 22:35:07 -0800 | [diff] [blame] | 295 | |
Pavel Emelyanov | b673e4d | 2007-11-19 22:36:45 -0800 | [diff] [blame] | 296 | read_lock(&raw_v4_hashinfo.lock); |
| 297 | raw_sk = sk_head(&raw_v4_hashinfo.ht[hash]); |
Ian Morris | 00db412 | 2015-04-03 09:17:27 +0100 | [diff] [blame] | 298 | if (raw_sk) { |
David Ahern | 6735993 | 2017-08-07 08:44:18 -0700 | [diff] [blame] | 299 | int dif = skb->dev->ifindex; |
| 300 | int sdif = inet_sdif(skb); |
| 301 | |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 302 | iph = (const struct iphdr *)skb->data; |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 303 | net = dev_net(skb->dev); |
Pavel Emelyanov | be185884 | 2008-01-14 05:35:31 -0800 | [diff] [blame] | 304 | |
| 305 | while ((raw_sk = __raw_v4_lookup(net, raw_sk, protocol, |
| 306 | iph->daddr, iph->saddr, |
David Ahern | 6735993 | 2017-08-07 08:44:18 -0700 | [diff] [blame] | 307 | dif, sdif)) != NULL) { |
Pavel Emelyanov | 7bc54c9 | 2007-11-19 22:35:07 -0800 | [diff] [blame] | 308 | raw_err(raw_sk, skb, info); |
| 309 | raw_sk = sk_next(raw_sk); |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 310 | iph = (const struct iphdr *)skb->data; |
Pavel Emelyanov | 7bc54c9 | 2007-11-19 22:35:07 -0800 | [diff] [blame] | 311 | } |
| 312 | } |
Pavel Emelyanov | b673e4d | 2007-11-19 22:36:45 -0800 | [diff] [blame] | 313 | read_unlock(&raw_v4_hashinfo.lock); |
Pavel Emelyanov | 7bc54c9 | 2007-11-19 22:35:07 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Daniel Baluta | 5e73ea1 | 2012-04-15 01:34:41 +0000 | [diff] [blame] | 316 | static int raw_rcv_skb(struct sock *sk, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | { |
| 318 | /* Charge it to the socket. */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 319 | |
Shawn Bohrer | fbf8866 | 2013-10-07 11:01:40 -0500 | [diff] [blame] | 320 | ipv4_pktinfo_prepare(sk, skb); |
Eric Dumazet | d826eb1 | 2011-11-09 07:24:35 +0000 | [diff] [blame] | 321 | if (sock_queue_rcv_skb(sk, skb) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | kfree_skb(skb); |
| 323 | return NET_RX_DROP; |
| 324 | } |
| 325 | |
| 326 | return NET_RX_SUCCESS; |
| 327 | } |
| 328 | |
| 329 | int raw_rcv(struct sock *sk, struct sk_buff *skb) |
| 330 | { |
| 331 | if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) { |
Wang Chen | 33c732c | 2007-11-13 20:30:01 -0800 | [diff] [blame] | 332 | atomic_inc(&sk->sk_drops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | kfree_skb(skb); |
| 334 | return NET_RX_DROP; |
| 335 | } |
Florian Westphal | 895b5c9 | 2019-09-29 20:54:03 +0200 | [diff] [blame] | 336 | nf_reset_ct(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 338 | skb_push(skb, skb->data - skb_network_header(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | |
| 340 | raw_rcv_skb(sk, skb); |
| 341 | return 0; |
| 342 | } |
| 343 | |
David S. Miller | 77968b7 | 2011-05-08 17:12:19 -0700 | [diff] [blame] | 344 | static int raw_send_hdrinc(struct sock *sk, struct flowi4 *fl4, |
Al Viro | 7ae9abf | 2014-11-27 19:30:51 -0500 | [diff] [blame] | 345 | struct msghdr *msg, size_t length, |
Soheil Hassas Yeganeh | c14ac94 | 2016-04-02 23:08:12 -0400 | [diff] [blame] | 346 | struct rtable **rtp, unsigned int flags, |
| 347 | const struct sockcm_cookie *sockc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | { |
| 349 | struct inet_sock *inet = inet_sk(sk); |
Pavel Emelyanov | 0388b00 | 2008-07-14 23:00:43 -0700 | [diff] [blame] | 350 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | struct iphdr *iph; |
| 352 | struct sk_buff *skb; |
Herbert Xu | f844c74 | 2008-01-05 23:14:44 -0800 | [diff] [blame] | 353 | unsigned int iphlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | int err; |
Eric Dumazet | 1789a64 | 2010-06-03 22:23:57 +0000 | [diff] [blame] | 355 | struct rtable *rt = *rtp; |
Herbert Xu | 6608824 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 356 | int hlen, tlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 358 | if (length > rt->dst.dev->mtu) { |
David S. Miller | 77968b7 | 2011-05-08 17:12:19 -0700 | [diff] [blame] | 359 | ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 360 | rt->dst.dev->mtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | return -EMSGSIZE; |
| 362 | } |
Alexander Potapenko | 86f4c90 | 2017-05-03 17:06:58 +0200 | [diff] [blame] | 363 | if (length < sizeof(struct iphdr)) |
| 364 | return -EINVAL; |
| 365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | if (flags&MSG_PROBE) |
| 367 | goto out; |
| 368 | |
Herbert Xu | 6608824 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 369 | hlen = LL_RESERVED_SPACE(rt->dst.dev); |
| 370 | tlen = rt->dst.dev->needed_tailroom; |
Johannes Berg | f5184d2 | 2008-05-12 20:48:31 -0700 | [diff] [blame] | 371 | skb = sock_alloc_send_skb(sk, |
Herbert Xu | 6608824 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 372 | length + hlen + tlen + 15, |
Johannes Berg | f5184d2 | 2008-05-12 20:48:31 -0700 | [diff] [blame] | 373 | flags & MSG_DONTWAIT, &err); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 374 | if (!skb) |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 375 | goto error; |
Herbert Xu | 6608824 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 376 | skb_reserve(skb, hlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | |
| 378 | skb->priority = sk->sk_priority; |
Willem de Bruijn | c6af0c2 | 2019-09-11 15:50:51 -0400 | [diff] [blame] | 379 | skb->mark = sockc->mark; |
Jesus Sanchez-Palencia | bc969a9 | 2018-07-03 15:42:49 -0700 | [diff] [blame] | 380 | skb->tstamp = sockc->transmit_time; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 381 | skb_dst_set(skb, &rt->dst); |
Eric Dumazet | 1789a64 | 2010-06-03 22:23:57 +0000 | [diff] [blame] | 382 | *rtp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
Arnaldo Carvalho de Melo | 7e28ecc | 2007-03-10 18:40:59 -0300 | [diff] [blame] | 384 | skb_reset_network_header(skb); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 385 | iph = ip_hdr(skb); |
Arnaldo Carvalho de Melo | 7e28ecc | 2007-03-10 18:40:59 -0300 | [diff] [blame] | 386 | skb_put(skb, length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | |
| 388 | skb->ip_summed = CHECKSUM_NONE; |
| 389 | |
Willem de Bruijn | 8f932f7 | 2018-12-17 12:24:00 -0500 | [diff] [blame] | 390 | skb_setup_tx_timestamp(skb, sockc->tsflags); |
Willem de Bruijn | 11878b4 | 2014-07-14 17:55:06 -0400 | [diff] [blame] | 391 | |
Julian Anastasov | 0dec879 | 2017-02-06 23:14:16 +0200 | [diff] [blame] | 392 | if (flags & MSG_CONFIRM) |
| 393 | skb_set_dst_pending_confirm(skb, 1); |
| 394 | |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 395 | skb->transport_header = skb->network_header; |
Neil Horman | 55888df | 2009-10-28 08:59:47 +0000 | [diff] [blame] | 396 | err = -EFAULT; |
Al Viro | 21226ab | 2014-11-28 15:48:29 -0500 | [diff] [blame] | 397 | if (memcpy_from_msg(iph, msg, length)) |
Neil Horman | 55888df | 2009-10-28 08:59:47 +0000 | [diff] [blame] | 398 | goto error_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | |
Herbert Xu | f844c74 | 2008-01-05 23:14:44 -0800 | [diff] [blame] | 400 | iphlen = iph->ihl * 4; |
Neil Horman | 55888df | 2009-10-28 08:59:47 +0000 | [diff] [blame] | 401 | |
| 402 | /* |
| 403 | * We don't want to modify the ip header, but we do need to |
| 404 | * be sure that it won't cause problems later along the network |
| 405 | * stack. Specifically we want to make sure that iph->ihl is a |
| 406 | * sane value. If ihl points beyond the length of the buffer passed |
| 407 | * in, reject the frame as invalid |
| 408 | */ |
| 409 | err = -EINVAL; |
| 410 | if (iphlen > length) |
| 411 | goto error_free; |
| 412 | |
| 413 | if (iphlen >= sizeof(*iph)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | if (!iph->saddr) |
David S. Miller | 77968b7 | 2011-05-08 17:12:19 -0700 | [diff] [blame] | 415 | iph->saddr = fl4->saddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | iph->check = 0; |
| 417 | iph->tot_len = htons(length); |
| 418 | if (!iph->id) |
Hannes Frederic Sowa | b6a7719 | 2015-03-25 17:07:44 +0100 | [diff] [blame] | 419 | ip_select_ident(net, skb, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
| 421 | iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); |
Ben Cartwright-Cox | 027ac58 | 2015-11-14 15:13:58 +0000 | [diff] [blame] | 422 | skb->transport_header += iphlen; |
| 423 | if (iph->protocol == IPPROTO_ICMP && |
| 424 | length >= iphlen + sizeof(struct icmphdr)) |
| 425 | icmp_out_count(net, ((struct icmphdr *) |
| 426 | skb_transport_header(skb))->type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 429 | err = NF_HOOK(NFPROTO_IPV4, NF_INET_LOCAL_OUT, |
| 430 | net, sk, skb, NULL, rt->dst.dev, |
Eric W. Biederman | 13206b6 | 2015-10-07 16:48:35 -0500 | [diff] [blame] | 431 | dst_output); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | if (err > 0) |
Eric Dumazet | 6ce9e7b | 2009-09-02 18:05:33 -0700 | [diff] [blame] | 433 | err = net_xmit_errno(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | if (err) |
| 435 | goto error; |
| 436 | out: |
| 437 | return 0; |
| 438 | |
Neil Horman | 55888df | 2009-10-28 08:59:47 +0000 | [diff] [blame] | 439 | error_free: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | kfree_skb(skb); |
| 441 | error: |
Pavel Emelyanov | 5e38e27 | 2008-07-16 20:19:49 -0700 | [diff] [blame] | 442 | IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS); |
Eric Dumazet | 6ce9e7b | 2009-09-02 18:05:33 -0700 | [diff] [blame] | 443 | if (err == -ENOBUFS && !inet->recverr) |
| 444 | err = 0; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 445 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Herbert Xu | c008ba5b | 2014-11-07 21:27:09 +0800 | [diff] [blame] | 448 | static int raw_probe_proto_opt(struct raw_frag_vec *rfv, struct flowi4 *fl4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | { |
Herbert Xu | 32b5913 | 2014-11-07 21:27:08 +0800 | [diff] [blame] | 450 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
Herbert Xu | 32b5913 | 2014-11-07 21:27:08 +0800 | [diff] [blame] | 452 | if (fl4->flowi4_proto != IPPROTO_ICMP) |
Heiko Carstens | a27b58f | 2006-10-30 15:06:12 -0800 | [diff] [blame] | 453 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
Herbert Xu | 32b5913 | 2014-11-07 21:27:08 +0800 | [diff] [blame] | 455 | /* We only need the first two bytes. */ |
Herbert Xu | c008ba5b | 2014-11-07 21:27:09 +0800 | [diff] [blame] | 456 | rfv->hlen = 2; |
| 457 | |
Al Viro | b61e9dc | 2014-11-24 10:52:29 -0500 | [diff] [blame] | 458 | err = memcpy_from_msg(rfv->hdr.c, rfv->msg, rfv->hlen); |
Herbert Xu | 32b5913 | 2014-11-07 21:27:08 +0800 | [diff] [blame] | 459 | if (err) |
| 460 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | |
Herbert Xu | c008ba5b | 2014-11-07 21:27:09 +0800 | [diff] [blame] | 462 | fl4->fl4_icmp_type = rfv->hdr.icmph.type; |
| 463 | fl4->fl4_icmp_code = rfv->hdr.icmph.code; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
Heiko Carstens | a27b58f | 2006-10-30 15:06:12 -0800 | [diff] [blame] | 465 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Herbert Xu | c008ba5b | 2014-11-07 21:27:09 +0800 | [diff] [blame] | 468 | static int raw_getfrag(void *from, char *to, int offset, int len, int odd, |
| 469 | struct sk_buff *skb) |
| 470 | { |
| 471 | struct raw_frag_vec *rfv = from; |
| 472 | |
| 473 | if (offset < rfv->hlen) { |
| 474 | int copy = min(rfv->hlen - offset, len); |
| 475 | |
| 476 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
| 477 | memcpy(to, rfv->hdr.c + offset, copy); |
| 478 | else |
| 479 | skb->csum = csum_block_add( |
| 480 | skb->csum, |
| 481 | csum_partial_copy_nocheck(rfv->hdr.c + offset, |
Al Viro | cc44c17 | 2020-07-11 00:12:07 -0400 | [diff] [blame] | 482 | to, copy), |
Herbert Xu | c008ba5b | 2014-11-07 21:27:09 +0800 | [diff] [blame] | 483 | odd); |
| 484 | |
| 485 | odd = 0; |
| 486 | offset += copy; |
| 487 | to += copy; |
| 488 | len -= copy; |
| 489 | |
| 490 | if (!len) |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | offset -= rfv->hlen; |
| 495 | |
Al Viro | f69e6d1 | 2014-11-24 13:23:40 -0500 | [diff] [blame] | 496 | return ip_generic_getfrag(rfv->msg, to, offset, len, odd, skb); |
Herbert Xu | c008ba5b | 2014-11-07 21:27:09 +0800 | [diff] [blame] | 497 | } |
| 498 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 499 | static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | { |
| 501 | struct inet_sock *inet = inet_sk(sk); |
David Ahern | bb191c3 | 2015-10-05 08:51:27 -0700 | [diff] [blame] | 502 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | struct ipcm_cookie ipc; |
| 504 | struct rtable *rt = NULL; |
David S. Miller | 77968b7 | 2011-05-08 17:12:19 -0700 | [diff] [blame] | 505 | struct flowi4 fl4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | int free = 0; |
Al Viro | 3ca3c68 | 2006-09-27 18:28:07 -0700 | [diff] [blame] | 507 | __be32 daddr; |
Al Viro | c1d18f9 | 2006-09-27 18:28:28 -0700 | [diff] [blame] | 508 | __be32 saddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | u8 tos; |
| 510 | int err; |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 511 | struct ip_options_data opt_copy; |
Herbert Xu | c008ba5b | 2014-11-07 21:27:09 +0800 | [diff] [blame] | 512 | struct raw_frag_vec rfv; |
Mohamed Ghannam | 8f659a0 | 2017-12-10 03:50:58 +0000 | [diff] [blame] | 513 | int hdrincl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
| 515 | err = -EMSGSIZE; |
Jesper Juhl | 926d4b8 | 2005-06-18 23:00:00 -0700 | [diff] [blame] | 516 | if (len > 0xFFFF) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | goto out; |
| 518 | |
Mohamed Ghannam | 8f659a0 | 2017-12-10 03:50:58 +0000 | [diff] [blame] | 519 | /* hdrincl should be READ_ONCE(inet->hdrincl) |
Nicolai Stange | 20b50d7 | 2018-01-08 15:54:44 +0100 | [diff] [blame] | 520 | * but READ_ONCE() doesn't work with bit fields. |
| 521 | * Doing this indirectly yields the same result. |
Mohamed Ghannam | 8f659a0 | 2017-12-10 03:50:58 +0000 | [diff] [blame] | 522 | */ |
| 523 | hdrincl = inet->hdrincl; |
Nicolai Stange | 20b50d7 | 2018-01-08 15:54:44 +0100 | [diff] [blame] | 524 | hdrincl = READ_ONCE(hdrincl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | /* |
| 526 | * Check the flags. |
| 527 | */ |
| 528 | |
| 529 | err = -EOPNOTSUPP; |
| 530 | if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message */ |
| 531 | goto out; /* compatibility */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 532 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | /* |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 534 | * Get and verify the address. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | */ |
| 536 | |
| 537 | if (msg->msg_namelen) { |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 538 | DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | err = -EINVAL; |
| 540 | if (msg->msg_namelen < sizeof(*usin)) |
| 541 | goto out; |
| 542 | if (usin->sin_family != AF_INET) { |
Joe Perches | 058bd4d | 2012-03-11 18:36:11 +0000 | [diff] [blame] | 543 | pr_info_once("%s: %s forgot to set AF_INET. Fix it!\n", |
| 544 | __func__, current->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | err = -EAFNOSUPPORT; |
| 546 | if (usin->sin_family) |
| 547 | goto out; |
| 548 | } |
| 549 | daddr = usin->sin_addr.s_addr; |
| 550 | /* ANK: I did not forget to get protocol from port field. |
| 551 | * I just do not know, who uses this weirdness. |
| 552 | * IP_HDRINCL is much more convenient. |
| 553 | */ |
| 554 | } else { |
| 555 | err = -EDESTADDRREQ; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 556 | if (sk->sk_state != TCP_ESTABLISHED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | goto out; |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 558 | daddr = inet->inet_daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Willem de Bruijn | 3517820 | 2018-07-06 10:12:54 -0400 | [diff] [blame] | 561 | ipcm_init_sk(&ipc, inet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | |
| 563 | if (msg->msg_controllen) { |
Soheil Hassas Yeganeh | 24025c4 | 2016-04-02 23:08:10 -0400 | [diff] [blame] | 564 | err = ip_cmsg_send(sk, msg, &ipc, false); |
Eric Dumazet | 9194830 | 2016-02-04 06:23:28 -0800 | [diff] [blame] | 565 | if (unlikely(err)) { |
| 566 | kfree(ipc.opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | goto out; |
Eric Dumazet | 9194830 | 2016-02-04 06:23:28 -0800 | [diff] [blame] | 568 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | if (ipc.opt) |
| 570 | free = 1; |
| 571 | } |
| 572 | |
| 573 | saddr = ipc.addr; |
| 574 | ipc.addr = daddr; |
| 575 | |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 576 | if (!ipc.opt) { |
| 577 | struct ip_options_rcu *inet_opt; |
| 578 | |
| 579 | rcu_read_lock(); |
| 580 | inet_opt = rcu_dereference(inet->inet_opt); |
| 581 | if (inet_opt) { |
| 582 | memcpy(&opt_copy, inet_opt, |
| 583 | sizeof(*inet_opt) + inet_opt->opt.optlen); |
| 584 | ipc.opt = &opt_copy.opt; |
| 585 | } |
| 586 | rcu_read_unlock(); |
| 587 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | |
| 589 | if (ipc.opt) { |
| 590 | err = -EINVAL; |
| 591 | /* Linux does not mangle headers on raw sockets, |
| 592 | * so that IP options + IP_HDRINCL is non-sense. |
| 593 | */ |
Mohamed Ghannam | 8f659a0 | 2017-12-10 03:50:58 +0000 | [diff] [blame] | 594 | if (hdrincl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | goto done; |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 596 | if (ipc.opt->opt.srr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | if (!daddr) |
| 598 | goto done; |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 599 | daddr = ipc.opt->opt.faddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | } |
| 601 | } |
Francesco Fusco | aa66158 | 2013-09-24 15:43:09 +0200 | [diff] [blame] | 602 | tos = get_rtconn_flags(&ipc, sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | if (msg->msg_flags & MSG_DONTROUTE) |
| 604 | tos |= RTO_ONLINK; |
| 605 | |
Joe Perches | f97c1e0 | 2007-12-16 13:45:43 -0800 | [diff] [blame] | 606 | if (ipv4_is_multicast(daddr)) { |
Robert Shearman | 854da99 | 2018-10-01 09:40:23 +0100 | [diff] [blame] | 607 | if (!ipc.oif || netif_index_is_l3_master(sock_net(sk), ipc.oif)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | ipc.oif = inet->mc_index; |
| 609 | if (!saddr) |
| 610 | saddr = inet->mc_addr; |
David Ahern | 9515a2e | 2018-01-24 19:37:38 -0800 | [diff] [blame] | 611 | } else if (!ipc.oif) { |
Erich E. Hoover | 76e2105 | 2012-02-08 09:11:07 +0000 | [diff] [blame] | 612 | ipc.oif = inet->uc_index; |
David Ahern | 9515a2e | 2018-01-24 19:37:38 -0800 | [diff] [blame] | 613 | } else if (ipv4_is_lbcast(daddr) && inet->uc_index) { |
Miaohe Lin | 645f089 | 2020-08-27 07:27:49 -0400 | [diff] [blame] | 614 | /* oif is set, packet is to local broadcast |
David Ahern | 9515a2e | 2018-01-24 19:37:38 -0800 | [diff] [blame] | 615 | * and uc_index is set. oif is most likely set |
| 616 | * by sk_bound_dev_if. If uc_index != oif check if the |
| 617 | * oif is an L3 master and uc_index is an L3 slave. |
| 618 | * If so, we want to allow the send using the uc_index. |
| 619 | */ |
| 620 | if (ipc.oif != inet->uc_index && |
| 621 | ipc.oif == l3mdev_master_ifindex_by_index(sock_net(sk), |
| 622 | inet->uc_index)) { |
| 623 | ipc.oif = inet->uc_index; |
| 624 | } |
| 625 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | |
Willem de Bruijn | c6af0c2 | 2019-09-11 15:50:51 -0400 | [diff] [blame] | 627 | flowi4_init_output(&fl4, ipc.oif, ipc.sockc.mark, tos, |
David S. Miller | 77968b7 | 2011-05-08 17:12:19 -0700 | [diff] [blame] | 628 | RT_SCOPE_UNIVERSE, |
Mohamed Ghannam | 8f659a0 | 2017-12-10 03:50:58 +0000 | [diff] [blame] | 629 | hdrincl ? IPPROTO_RAW : sk->sk_protocol, |
Steffen Klassert | 0e0d44a | 2013-08-28 08:04:14 +0200 | [diff] [blame] | 630 | inet_sk_flowi_flags(sk) | |
Mohamed Ghannam | 8f659a0 | 2017-12-10 03:50:58 +0000 | [diff] [blame] | 631 | (hdrincl ? FLOWI_FLAG_KNOWN_NH : 0), |
Lorenzo Colitti | e2d118a | 2016-11-04 02:23:43 +0900 | [diff] [blame] | 632 | daddr, saddr, 0, 0, sk->sk_uid); |
David S. Miller | ef164ae | 2011-03-31 04:53:51 -0700 | [diff] [blame] | 633 | |
Mohamed Ghannam | 8f659a0 | 2017-12-10 03:50:58 +0000 | [diff] [blame] | 634 | if (!hdrincl) { |
Al Viro | b61e9dc | 2014-11-24 10:52:29 -0500 | [diff] [blame] | 635 | rfv.msg = msg; |
Herbert Xu | c008ba5b | 2014-11-07 21:27:09 +0800 | [diff] [blame] | 636 | rfv.hlen = 0; |
| 637 | |
| 638 | err = raw_probe_proto_opt(&rfv, &fl4); |
David S. Miller | 77968b7 | 2011-05-08 17:12:19 -0700 | [diff] [blame] | 639 | if (err) |
David S. Miller | b23dd4f | 2011-03-02 14:31:35 -0800 | [diff] [blame] | 640 | goto done; |
David S. Miller | 77968b7 | 2011-05-08 17:12:19 -0700 | [diff] [blame] | 641 | } |
| 642 | |
Paul Moore | 3df98d7 | 2020-09-27 22:38:26 -0400 | [diff] [blame] | 643 | security_sk_classify_flow(sk, flowi4_to_flowi_common(&fl4)); |
David Ahern | bb191c3 | 2015-10-05 08:51:27 -0700 | [diff] [blame] | 644 | rt = ip_route_output_flow(net, &fl4, sk); |
David S. Miller | 77968b7 | 2011-05-08 17:12:19 -0700 | [diff] [blame] | 645 | if (IS_ERR(rt)) { |
| 646 | err = PTR_ERR(rt); |
| 647 | rt = NULL; |
| 648 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | |
| 651 | err = -EACCES; |
| 652 | if (rt->rt_flags & RTCF_BROADCAST && !sock_flag(sk, SOCK_BROADCAST)) |
| 653 | goto done; |
| 654 | |
| 655 | if (msg->msg_flags & MSG_CONFIRM) |
| 656 | goto do_confirm; |
| 657 | back_from_confirm: |
| 658 | |
Mohamed Ghannam | 8f659a0 | 2017-12-10 03:50:58 +0000 | [diff] [blame] | 659 | if (hdrincl) |
Al Viro | 7ae9abf | 2014-11-27 19:30:51 -0500 | [diff] [blame] | 660 | err = raw_send_hdrinc(sk, &fl4, msg, len, |
Soheil Hassas Yeganeh | c14ac94 | 2016-04-02 23:08:12 -0400 | [diff] [blame] | 661 | &rt, msg->msg_flags, &ipc.sockc); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 662 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | else { |
| 664 | if (!ipc.addr) |
David S. Miller | 77968b7 | 2011-05-08 17:12:19 -0700 | [diff] [blame] | 665 | ipc.addr = fl4.daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | lock_sock(sk); |
Herbert Xu | c008ba5b | 2014-11-07 21:27:09 +0800 | [diff] [blame] | 667 | err = ip_append_data(sk, &fl4, raw_getfrag, |
| 668 | &rfv, len, 0, |
David S. Miller | 77968b7 | 2011-05-08 17:12:19 -0700 | [diff] [blame] | 669 | &ipc, &rt, msg->msg_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | if (err) |
| 671 | ip_flush_pending_frames(sk); |
Eric Dumazet | 6ce9e7b | 2009-09-02 18:05:33 -0700 | [diff] [blame] | 672 | else if (!(msg->msg_flags & MSG_MORE)) { |
David S. Miller | 77968b7 | 2011-05-08 17:12:19 -0700 | [diff] [blame] | 673 | err = ip_push_pending_frames(sk, &fl4); |
Eric Dumazet | 6ce9e7b | 2009-09-02 18:05:33 -0700 | [diff] [blame] | 674 | if (err == -ENOBUFS && !inet->recverr) |
| 675 | err = 0; |
| 676 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | release_sock(sk); |
| 678 | } |
| 679 | done: |
| 680 | if (free) |
| 681 | kfree(ipc.opt); |
| 682 | ip_rt_put(rt); |
| 683 | |
Jesper Juhl | 5418c692 | 2005-06-18 22:59:45 -0700 | [diff] [blame] | 684 | out: |
| 685 | if (err < 0) |
| 686 | return err; |
| 687 | return len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | |
| 689 | do_confirm: |
Julian Anastasov | 0dec879 | 2017-02-06 23:14:16 +0200 | [diff] [blame] | 690 | if (msg->msg_flags & MSG_PROBE) |
| 691 | dst_confirm_neigh(&rt->dst, &fl4.daddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | if (!(msg->msg_flags & MSG_PROBE) || len) |
| 693 | goto back_from_confirm; |
| 694 | err = 0; |
| 695 | goto done; |
| 696 | } |
| 697 | |
| 698 | static void raw_close(struct sock *sk, long timeout) |
| 699 | { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 700 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 701 | * Raw sockets may have direct kernel references. Kill them. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | */ |
| 703 | ip_ra_control(sk, 0, NULL); |
| 704 | |
| 705 | sk_common_release(sk); |
| 706 | } |
| 707 | |
Brian Haley | 7d06b2e | 2008-06-14 17:04:49 -0700 | [diff] [blame] | 708 | static void raw_destroy(struct sock *sk) |
Denis V. Lunev | 22dd485 | 2008-06-04 15:16:12 -0700 | [diff] [blame] | 709 | { |
| 710 | lock_sock(sk); |
| 711 | ip_flush_pending_frames(sk); |
| 712 | release_sock(sk); |
Denis V. Lunev | 22dd485 | 2008-06-04 15:16:12 -0700 | [diff] [blame] | 713 | } |
| 714 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | /* This gets rid of all the nasties in af_inet. -DaveM */ |
| 716 | static int raw_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len) |
| 717 | { |
| 718 | struct inet_sock *inet = inet_sk(sk); |
| 719 | struct sockaddr_in *addr = (struct sockaddr_in *) uaddr; |
Riccardo Paolo Bestetti | 8ff978b | 2021-11-17 10:00:11 +0100 | [diff] [blame] | 720 | struct net *net = sock_net(sk); |
David Ahern | cd2c0f4 | 2016-11-03 09:25:00 -0700 | [diff] [blame] | 721 | u32 tb_id = RT_TABLE_LOCAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | int ret = -EINVAL; |
| 723 | int chk_addr_ret; |
| 724 | |
| 725 | if (sk->sk_state != TCP_CLOSE || addr_len < sizeof(struct sockaddr_in)) |
| 726 | goto out; |
David Ahern | cd2c0f4 | 2016-11-03 09:25:00 -0700 | [diff] [blame] | 727 | |
| 728 | if (sk->sk_bound_dev_if) |
Riccardo Paolo Bestetti | 8ff978b | 2021-11-17 10:00:11 +0100 | [diff] [blame] | 729 | tb_id = l3mdev_fib_table_by_index(net, |
| 730 | sk->sk_bound_dev_if) ? : tb_id; |
David Ahern | cd2c0f4 | 2016-11-03 09:25:00 -0700 | [diff] [blame] | 731 | |
Riccardo Paolo Bestetti | 8ff978b | 2021-11-17 10:00:11 +0100 | [diff] [blame] | 732 | chk_addr_ret = inet_addr_type_table(net, addr->sin_addr.s_addr, tb_id); |
David Ahern | cd2c0f4 | 2016-11-03 09:25:00 -0700 | [diff] [blame] | 733 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | ret = -EADDRNOTAVAIL; |
Riccardo Paolo Bestetti | 8ff978b | 2021-11-17 10:00:11 +0100 | [diff] [blame] | 735 | if (!inet_addr_valid_or_nonlocal(net, inet, addr->sin_addr.s_addr, |
| 736 | chk_addr_ret)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | goto out; |
Riccardo Paolo Bestetti | 8ff978b | 2021-11-17 10:00:11 +0100 | [diff] [blame] | 738 | |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 739 | inet->inet_rcv_saddr = inet->inet_saddr = addr->sin_addr.s_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST) |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 741 | inet->inet_saddr = 0; /* Use device */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | sk_dst_reset(sk); |
| 743 | ret = 0; |
| 744 | out: return ret; |
| 745 | } |
| 746 | |
| 747 | /* |
| 748 | * This should be easy, if there is something there |
| 749 | * we return it, otherwise we block. |
| 750 | */ |
| 751 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 752 | static int raw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, |
| 753 | int noblock, int flags, int *addr_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | { |
| 755 | struct inet_sock *inet = inet_sk(sk); |
| 756 | size_t copied = 0; |
| 757 | int err = -EOPNOTSUPP; |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 758 | DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | struct sk_buff *skb; |
| 760 | |
| 761 | if (flags & MSG_OOB) |
| 762 | goto out; |
| 763 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | if (flags & MSG_ERRQUEUE) { |
Hannes Frederic Sowa | 85fbaa7 | 2013-11-23 00:46:12 +0100 | [diff] [blame] | 765 | err = ip_recv_error(sk, msg, len, addr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | goto out; |
| 767 | } |
| 768 | |
| 769 | skb = skb_recv_datagram(sk, flags, noblock, &err); |
| 770 | if (!skb) |
| 771 | goto out; |
| 772 | |
| 773 | copied = skb->len; |
| 774 | if (len < copied) { |
| 775 | msg->msg_flags |= MSG_TRUNC; |
| 776 | copied = len; |
| 777 | } |
| 778 | |
David S. Miller | 51f3d02 | 2014-11-05 16:46:40 -0500 | [diff] [blame] | 779 | err = skb_copy_datagram_msg(skb, 0, msg, copied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | if (err) |
| 781 | goto done; |
| 782 | |
Neil Horman | 3b88578 | 2009-10-12 13:26:31 -0700 | [diff] [blame] | 783 | sock_recv_ts_and_drops(msg, sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | |
| 785 | /* Copy the address. */ |
| 786 | if (sin) { |
| 787 | sin->sin_family = AF_INET; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 788 | sin->sin_addr.s_addr = ip_hdr(skb)->saddr; |
Tetsuo Handa | f59fc7f | 2006-07-25 17:05:35 -0700 | [diff] [blame] | 789 | sin->sin_port = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); |
Hannes Frederic Sowa | bceaa90 | 2013-11-18 04:20:45 +0100 | [diff] [blame] | 791 | *addr_len = sizeof(*sin); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | } |
| 793 | if (inet->cmsg_flags) |
| 794 | ip_cmsg_recv(msg, skb); |
| 795 | if (flags & MSG_TRUNC) |
| 796 | copied = skb->len; |
| 797 | done: |
| 798 | skb_free_datagram(sk, skb); |
Jesper Juhl | 5418c692 | 2005-06-18 22:59:45 -0700 | [diff] [blame] | 799 | out: |
| 800 | if (err) |
| 801 | return err; |
| 802 | return copied; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | } |
| 804 | |
Mike Manning | 6897445 | 2018-11-07 15:36:05 +0000 | [diff] [blame] | 805 | static int raw_sk_init(struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | { |
| 807 | struct raw_sock *rp = raw_sk(sk); |
| 808 | |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 809 | if (inet_sk(sk)->inet_num == IPPROTO_ICMP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | memset(&rp->filter, 0, sizeof(rp->filter)); |
| 811 | return 0; |
| 812 | } |
| 813 | |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 814 | static int raw_seticmpfilter(struct sock *sk, sockptr_t optval, int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | { |
| 816 | if (optlen > sizeof(struct icmp_filter)) |
| 817 | optlen = sizeof(struct icmp_filter); |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 818 | if (copy_from_sockptr(&raw_sk(sk)->filter, optval, optlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | return -EFAULT; |
| 820 | return 0; |
| 821 | } |
| 822 | |
| 823 | static int raw_geticmpfilter(struct sock *sk, char __user *optval, int __user *optlen) |
| 824 | { |
| 825 | int len, ret = -EFAULT; |
| 826 | |
| 827 | if (get_user(len, optlen)) |
| 828 | goto out; |
| 829 | ret = -EINVAL; |
| 830 | if (len < 0) |
| 831 | goto out; |
| 832 | if (len > sizeof(struct icmp_filter)) |
| 833 | len = sizeof(struct icmp_filter); |
| 834 | ret = -EFAULT; |
| 835 | if (put_user(len, optlen) || |
| 836 | copy_to_user(optval, &raw_sk(sk)->filter, len)) |
| 837 | goto out; |
| 838 | ret = 0; |
| 839 | out: return ret; |
| 840 | } |
| 841 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 842 | static int do_raw_setsockopt(struct sock *sk, int level, int optname, |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 843 | sockptr_t optval, unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | if (optname == ICMP_FILTER) { |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 846 | if (inet_sk(sk)->inet_num != IPPROTO_ICMP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | return -EOPNOTSUPP; |
| 848 | else |
| 849 | return raw_seticmpfilter(sk, optval, optlen); |
| 850 | } |
| 851 | return -ENOPROTOOPT; |
| 852 | } |
| 853 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 854 | static int raw_setsockopt(struct sock *sk, int level, int optname, |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 855 | sockptr_t optval, unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | { |
| 857 | if (level != SOL_RAW) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 858 | return ip_setsockopt(sk, level, optname, optval, optlen); |
| 859 | return do_raw_setsockopt(sk, level, optname, optval, optlen); |
| 860 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 862 | static int do_raw_getsockopt(struct sock *sk, int level, int optname, |
| 863 | char __user *optval, int __user *optlen) |
| 864 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | if (optname == ICMP_FILTER) { |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 866 | if (inet_sk(sk)->inet_num != IPPROTO_ICMP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | return -EOPNOTSUPP; |
| 868 | else |
| 869 | return raw_geticmpfilter(sk, optval, optlen); |
| 870 | } |
| 871 | return -ENOPROTOOPT; |
| 872 | } |
| 873 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 874 | static int raw_getsockopt(struct sock *sk, int level, int optname, |
| 875 | char __user *optval, int __user *optlen) |
| 876 | { |
| 877 | if (level != SOL_RAW) |
| 878 | return ip_getsockopt(sk, level, optname, optval, optlen); |
| 879 | return do_raw_getsockopt(sk, level, optname, optval, optlen); |
| 880 | } |
| 881 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | static int raw_ioctl(struct sock *sk, int cmd, unsigned long arg) |
| 883 | { |
| 884 | switch (cmd) { |
Joe Perches | 4500ebf | 2011-07-01 09:43:07 +0000 | [diff] [blame] | 885 | case SIOCOUTQ: { |
| 886 | int amount = sk_wmem_alloc_get(sk); |
Eric Dumazet | 31e6d36 | 2009-06-17 19:05:41 -0700 | [diff] [blame] | 887 | |
Joe Perches | 4500ebf | 2011-07-01 09:43:07 +0000 | [diff] [blame] | 888 | return put_user(amount, (int __user *)arg); |
| 889 | } |
| 890 | case SIOCINQ: { |
| 891 | struct sk_buff *skb; |
| 892 | int amount = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | |
Joe Perches | 4500ebf | 2011-07-01 09:43:07 +0000 | [diff] [blame] | 894 | spin_lock_bh(&sk->sk_receive_queue.lock); |
| 895 | skb = skb_peek(&sk->sk_receive_queue); |
Ian Morris | 00db412 | 2015-04-03 09:17:27 +0100 | [diff] [blame] | 896 | if (skb) |
Joe Perches | 4500ebf | 2011-07-01 09:43:07 +0000 | [diff] [blame] | 897 | amount = skb->len; |
| 898 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
| 899 | return put_user(amount, (int __user *)arg); |
| 900 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | |
Joe Perches | 4500ebf | 2011-07-01 09:43:07 +0000 | [diff] [blame] | 902 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | #ifdef CONFIG_IP_MROUTE |
Joe Perches | 4500ebf | 2011-07-01 09:43:07 +0000 | [diff] [blame] | 904 | return ipmr_ioctl(sk, cmd, (void __user *)arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | #else |
Joe Perches | 4500ebf | 2011-07-01 09:43:07 +0000 | [diff] [blame] | 906 | return -ENOIOCTLCMD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | #endif |
| 908 | } |
| 909 | } |
| 910 | |
Eric W. Biederman | 709b46e | 2011-01-29 16:15:56 +0000 | [diff] [blame] | 911 | #ifdef CONFIG_COMPAT |
| 912 | static int compat_raw_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg) |
| 913 | { |
| 914 | switch (cmd) { |
| 915 | case SIOCOUTQ: |
| 916 | case SIOCINQ: |
| 917 | return -ENOIOCTLCMD; |
| 918 | default: |
| 919 | #ifdef CONFIG_IP_MROUTE |
| 920 | return ipmr_compat_ioctl(sk, cmd, compat_ptr(arg)); |
| 921 | #else |
| 922 | return -ENOIOCTLCMD; |
| 923 | #endif |
| 924 | } |
| 925 | } |
| 926 | #endif |
| 927 | |
Cyrill Gorcunov | 432490f | 2016-10-21 13:03:44 +0300 | [diff] [blame] | 928 | int raw_abort(struct sock *sk, int err) |
| 929 | { |
| 930 | lock_sock(sk); |
| 931 | |
| 932 | sk->sk_err = err; |
Alexander Aring | e3ae236 | 2021-06-27 18:48:21 -0400 | [diff] [blame] | 933 | sk_error_report(sk); |
Cyrill Gorcunov | 3de864f | 2016-11-01 23:05:00 +0300 | [diff] [blame] | 934 | __udp_disconnect(sk, 0); |
Cyrill Gorcunov | 432490f | 2016-10-21 13:03:44 +0300 | [diff] [blame] | 935 | |
| 936 | release_sock(sk); |
| 937 | |
| 938 | return 0; |
| 939 | } |
| 940 | EXPORT_SYMBOL_GPL(raw_abort); |
| 941 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | struct proto raw_prot = { |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 943 | .name = "RAW", |
| 944 | .owner = THIS_MODULE, |
| 945 | .close = raw_close, |
Denis V. Lunev | 22dd485 | 2008-06-04 15:16:12 -0700 | [diff] [blame] | 946 | .destroy = raw_destroy, |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 947 | .connect = ip4_datagram_connect, |
Eric Dumazet | 286c72d | 2016-10-20 09:39:40 -0700 | [diff] [blame] | 948 | .disconnect = __udp_disconnect, |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 949 | .ioctl = raw_ioctl, |
Mike Manning | 6897445 | 2018-11-07 15:36:05 +0000 | [diff] [blame] | 950 | .init = raw_sk_init, |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 951 | .setsockopt = raw_setsockopt, |
| 952 | .getsockopt = raw_getsockopt, |
| 953 | .sendmsg = raw_sendmsg, |
| 954 | .recvmsg = raw_recvmsg, |
| 955 | .bind = raw_bind, |
| 956 | .backlog_rcv = raw_rcv_skb, |
Steffen Klassert | 8141ed9 | 2013-01-21 02:00:03 +0000 | [diff] [blame] | 957 | .release_cb = ip4_datagram_release_cb, |
Pavel Emelyanov | fc8717b | 2008-03-22 16:56:51 -0700 | [diff] [blame] | 958 | .hash = raw_hash_sk, |
| 959 | .unhash = raw_unhash_sk, |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 960 | .obj_size = sizeof(struct raw_sock), |
David Windsor | 8c2bc89 | 2017-08-24 16:49:14 -0700 | [diff] [blame] | 961 | .useroffset = offsetof(struct raw_sock, filter), |
| 962 | .usersize = sizeof_field(struct raw_sock, filter), |
Pavel Emelyanov | fc8717b | 2008-03-22 16:56:51 -0700 | [diff] [blame] | 963 | .h.raw_hash = &raw_v4_hashinfo, |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 964 | #ifdef CONFIG_COMPAT |
Eric W. Biederman | 709b46e | 2011-01-29 16:15:56 +0000 | [diff] [blame] | 965 | .compat_ioctl = compat_raw_ioctl, |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 966 | #endif |
Cyrill Gorcunov | 432490f | 2016-10-21 13:03:44 +0300 | [diff] [blame] | 967 | .diag_destroy = raw_abort, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | }; |
| 969 | |
| 970 | #ifdef CONFIG_PROC_FS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | static struct sock *raw_get_first(struct seq_file *seq) |
| 972 | { |
| 973 | struct sock *sk; |
Muchun Song | 359745d | 2022-01-21 22:14:23 -0800 | [diff] [blame] | 974 | struct raw_hashinfo *h = pde_data(file_inode(seq->file)); |
Jianjun Kong | 5a5f3a8 | 2008-11-03 00:24:34 -0800 | [diff] [blame] | 975 | struct raw_iter_state *state = raw_seq_private(seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | |
Pavel Emelyanov | b673e4d | 2007-11-19 22:36:45 -0800 | [diff] [blame] | 977 | for (state->bucket = 0; state->bucket < RAW_HTABLE_SIZE; |
| 978 | ++state->bucket) { |
Christoph Hellwig | 93cb5a1 | 2018-04-10 22:08:28 +0200 | [diff] [blame] | 979 | sk_for_each(sk, &h->ht[state->bucket]) |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 980 | if (sock_net(sk) == seq_file_net(seq)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | goto found; |
| 982 | } |
| 983 | sk = NULL; |
| 984 | found: |
| 985 | return sk; |
| 986 | } |
| 987 | |
| 988 | static struct sock *raw_get_next(struct seq_file *seq, struct sock *sk) |
| 989 | { |
Muchun Song | 359745d | 2022-01-21 22:14:23 -0800 | [diff] [blame] | 990 | struct raw_hashinfo *h = pde_data(file_inode(seq->file)); |
Jianjun Kong | 5a5f3a8 | 2008-11-03 00:24:34 -0800 | [diff] [blame] | 991 | struct raw_iter_state *state = raw_seq_private(seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | |
| 993 | do { |
| 994 | sk = sk_next(sk); |
| 995 | try_again: |
| 996 | ; |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 997 | } while (sk && sock_net(sk) != seq_file_net(seq)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | |
Pavel Emelyanov | b673e4d | 2007-11-19 22:36:45 -0800 | [diff] [blame] | 999 | if (!sk && ++state->bucket < RAW_HTABLE_SIZE) { |
Christoph Hellwig | 93cb5a1 | 2018-04-10 22:08:28 +0200 | [diff] [blame] | 1000 | sk = sk_head(&h->ht[state->bucket]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | goto try_again; |
| 1002 | } |
| 1003 | return sk; |
| 1004 | } |
| 1005 | |
| 1006 | static struct sock *raw_get_idx(struct seq_file *seq, loff_t pos) |
| 1007 | { |
| 1008 | struct sock *sk = raw_get_first(seq); |
| 1009 | |
| 1010 | if (sk) |
| 1011 | while (pos && (sk = raw_get_next(seq, sk)) != NULL) |
| 1012 | --pos; |
| 1013 | return pos ? NULL : sk; |
| 1014 | } |
| 1015 | |
Pavel Emelyanov | 42a7380 | 2007-11-19 22:38:33 -0800 | [diff] [blame] | 1016 | void *raw_seq_start(struct seq_file *seq, loff_t *pos) |
Jules Irenge | 0d8a42c | 2020-03-11 01:09:02 +0000 | [diff] [blame] | 1017 | __acquires(&h->lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | { |
Muchun Song | 359745d | 2022-01-21 22:14:23 -0800 | [diff] [blame] | 1019 | struct raw_hashinfo *h = pde_data(file_inode(seq->file)); |
Pavel Emelyanov | 42a7380 | 2007-11-19 22:38:33 -0800 | [diff] [blame] | 1020 | |
Christoph Hellwig | 93cb5a1 | 2018-04-10 22:08:28 +0200 | [diff] [blame] | 1021 | read_lock(&h->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | return *pos ? raw_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; |
| 1023 | } |
Pavel Emelyanov | 42a7380 | 2007-11-19 22:38:33 -0800 | [diff] [blame] | 1024 | EXPORT_SYMBOL_GPL(raw_seq_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | |
Pavel Emelyanov | 42a7380 | 2007-11-19 22:38:33 -0800 | [diff] [blame] | 1026 | void *raw_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | { |
| 1028 | struct sock *sk; |
| 1029 | |
| 1030 | if (v == SEQ_START_TOKEN) |
| 1031 | sk = raw_get_first(seq); |
| 1032 | else |
| 1033 | sk = raw_get_next(seq, v); |
| 1034 | ++*pos; |
| 1035 | return sk; |
| 1036 | } |
Pavel Emelyanov | 42a7380 | 2007-11-19 22:38:33 -0800 | [diff] [blame] | 1037 | EXPORT_SYMBOL_GPL(raw_seq_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | |
Pavel Emelyanov | 42a7380 | 2007-11-19 22:38:33 -0800 | [diff] [blame] | 1039 | void raw_seq_stop(struct seq_file *seq, void *v) |
Jules Irenge | 0d8a42c | 2020-03-11 01:09:02 +0000 | [diff] [blame] | 1040 | __releases(&h->lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | { |
Muchun Song | 359745d | 2022-01-21 22:14:23 -0800 | [diff] [blame] | 1042 | struct raw_hashinfo *h = pde_data(file_inode(seq->file)); |
Pavel Emelyanov | 42a7380 | 2007-11-19 22:38:33 -0800 | [diff] [blame] | 1043 | |
Christoph Hellwig | 93cb5a1 | 2018-04-10 22:08:28 +0200 | [diff] [blame] | 1044 | read_unlock(&h->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | } |
Pavel Emelyanov | 42a7380 | 2007-11-19 22:38:33 -0800 | [diff] [blame] | 1046 | EXPORT_SYMBOL_GPL(raw_seq_stop); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | |
Denis V. Lunev | 8cd850e | 2008-01-31 03:46:43 -0800 | [diff] [blame] | 1048 | static void raw_sock_seq_show(struct seq_file *seq, struct sock *sp, int i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | { |
| 1050 | struct inet_sock *inet = inet_sk(sp); |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 1051 | __be32 dest = inet->inet_daddr, |
| 1052 | src = inet->inet_rcv_saddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | __u16 destp = 0, |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 1054 | srcp = inet->inet_num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | |
Denis V. Lunev | 8cd850e | 2008-01-31 03:46:43 -0800 | [diff] [blame] | 1056 | seq_printf(seq, "%4d: %08X:%04X %08X:%04X" |
Patrick Talbert | ea9a037 | 2019-05-17 17:11:28 +0200 | [diff] [blame] | 1057 | " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %u\n", |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1058 | i, src, srcp, dest, destp, sp->sk_state, |
Eric Dumazet | 31e6d36 | 2009-06-17 19:05:41 -0700 | [diff] [blame] | 1059 | sk_wmem_alloc_get(sp), |
| 1060 | sk_rmem_alloc_get(sp), |
Eric W. Biederman | a7cb5a4 | 2012-05-24 01:10:10 -0600 | [diff] [blame] | 1061 | 0, 0L, 0, |
| 1062 | from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)), |
| 1063 | 0, sock_i_ino(sp), |
Reshetova, Elena | 41c6d65 | 2017-06-30 13:08:01 +0300 | [diff] [blame] | 1064 | refcount_read(&sp->sk_refcnt), sp, atomic_read(&sp->sk_drops)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | static int raw_seq_show(struct seq_file *seq, void *v) |
| 1068 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | if (v == SEQ_START_TOKEN) |
Denis V. Lunev | 8cd850e | 2008-01-31 03:46:43 -0800 | [diff] [blame] | 1070 | seq_printf(seq, " sl local_address rem_address st tx_queue " |
| 1071 | "rx_queue tr tm->when retrnsmt uid timeout " |
Eric Dumazet | cb61cb9 | 2008-06-17 21:04:56 -0700 | [diff] [blame] | 1072 | "inode ref pointer drops\n"); |
Denis V. Lunev | 8cd850e | 2008-01-31 03:46:43 -0800 | [diff] [blame] | 1073 | else |
| 1074 | raw_sock_seq_show(seq, v, raw_seq_private(seq)->bucket); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | return 0; |
| 1076 | } |
| 1077 | |
Stephen Hemminger | f690808 | 2007-03-12 14:34:29 -0700 | [diff] [blame] | 1078 | static const struct seq_operations raw_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | .start = raw_seq_start, |
| 1080 | .next = raw_seq_next, |
| 1081 | .stop = raw_seq_stop, |
| 1082 | .show = raw_seq_show, |
| 1083 | }; |
| 1084 | |
Pavel Emelyanov | a308da1 | 2008-01-14 05:36:50 -0800 | [diff] [blame] | 1085 | static __net_init int raw_init_net(struct net *net) |
| 1086 | { |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 1087 | if (!proc_create_net_data("raw", 0444, net->proc_net, &raw_seq_ops, |
| 1088 | sizeof(struct raw_iter_state), &raw_v4_hashinfo)) |
Pavel Emelyanov | a308da1 | 2008-01-14 05:36:50 -0800 | [diff] [blame] | 1089 | return -ENOMEM; |
| 1090 | |
| 1091 | return 0; |
| 1092 | } |
| 1093 | |
| 1094 | static __net_exit void raw_exit_net(struct net *net) |
| 1095 | { |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 1096 | remove_proc_entry("raw", net->proc_net); |
Pavel Emelyanov | a308da1 | 2008-01-14 05:36:50 -0800 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | static __net_initdata struct pernet_operations raw_net_ops = { |
| 1100 | .init = raw_init_net, |
| 1101 | .exit = raw_exit_net, |
| 1102 | }; |
| 1103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | int __init raw_proc_init(void) |
| 1105 | { |
Pavel Emelyanov | a308da1 | 2008-01-14 05:36:50 -0800 | [diff] [blame] | 1106 | return register_pernet_subsys(&raw_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | void __init raw_proc_exit(void) |
| 1110 | { |
Pavel Emelyanov | a308da1 | 2008-01-14 05:36:50 -0800 | [diff] [blame] | 1111 | unregister_pernet_subsys(&raw_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | } |
David Ahern | 86d1d8b | 2018-11-27 20:52:24 -0800 | [diff] [blame] | 1113 | #endif /* CONFIG_PROC_FS */ |
Mike Manning | 6897445 | 2018-11-07 15:36:05 +0000 | [diff] [blame] | 1114 | |
| 1115 | static void raw_sysctl_init_net(struct net *net) |
| 1116 | { |
| 1117 | #ifdef CONFIG_NET_L3_MASTER_DEV |
| 1118 | net->ipv4.sysctl_raw_l3mdev_accept = 1; |
| 1119 | #endif |
| 1120 | } |
| 1121 | |
| 1122 | static int __net_init raw_sysctl_init(struct net *net) |
| 1123 | { |
| 1124 | raw_sysctl_init_net(net); |
| 1125 | return 0; |
| 1126 | } |
| 1127 | |
| 1128 | static struct pernet_operations __net_initdata raw_sysctl_ops = { |
| 1129 | .init = raw_sysctl_init, |
| 1130 | }; |
| 1131 | |
| 1132 | void __init raw_init(void) |
| 1133 | { |
| 1134 | raw_sysctl_init_net(&init_net); |
| 1135 | if (register_pernet_subsys(&raw_sysctl_ops)) |
| 1136 | panic("RAW: failed to init sysctl parameters.\n"); |
| 1137 | } |