0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 #ifndef _UDP_H
0019 #define _UDP_H
0020
0021 #include <linux/list.h>
0022 #include <linux/bug.h>
0023 #include <net/inet_sock.h>
0024 #include <net/sock.h>
0025 #include <net/snmp.h>
0026 #include <net/ip.h>
0027 #include <linux/ipv6.h>
0028 #include <linux/seq_file.h>
0029 #include <linux/poll.h>
0030 #include <linux/indirect_call_wrapper.h>
0031
0032
0033
0034
0035
0036
0037
0038
0039 struct udp_skb_cb {
0040 union {
0041 struct inet_skb_parm h4;
0042 #if IS_ENABLED(CONFIG_IPV6)
0043 struct inet6_skb_parm h6;
0044 #endif
0045 } header;
0046 __u16 cscov;
0047 __u8 partial_cov;
0048 };
0049 #define UDP_SKB_CB(__skb) ((struct udp_skb_cb *)((__skb)->cb))
0050
0051
0052
0053
0054
0055
0056
0057
0058 struct udp_hslot {
0059 struct hlist_head head;
0060 int count;
0061 spinlock_t lock;
0062 } __attribute__((aligned(2 * sizeof(long))));
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072 struct udp_table {
0073 struct udp_hslot *hash;
0074 struct udp_hslot *hash2;
0075 unsigned int mask;
0076 unsigned int log;
0077 };
0078 extern struct udp_table udp_table;
0079 void udp_table_init(struct udp_table *, const char *);
0080 static inline struct udp_hslot *udp_hashslot(struct udp_table *table,
0081 struct net *net, unsigned int num)
0082 {
0083 return &table->hash[udp_hashfn(net, num, table->mask)];
0084 }
0085
0086
0087
0088
0089 static inline struct udp_hslot *udp_hashslot2(struct udp_table *table,
0090 unsigned int hash)
0091 {
0092 return &table->hash2[hash & table->mask];
0093 }
0094
0095 extern struct proto udp_prot;
0096
0097 extern atomic_long_t udp_memory_allocated;
0098 DECLARE_PER_CPU(int, udp_memory_per_cpu_fw_alloc);
0099
0100
0101 extern long sysctl_udp_mem[3];
0102 extern int sysctl_udp_rmem_min;
0103 extern int sysctl_udp_wmem_min;
0104
0105 struct sk_buff;
0106
0107
0108
0109
0110 static inline __sum16 __udp_lib_checksum_complete(struct sk_buff *skb)
0111 {
0112 return (UDP_SKB_CB(skb)->cscov == skb->len ?
0113 __skb_checksum_complete(skb) :
0114 __skb_checksum_complete_head(skb, UDP_SKB_CB(skb)->cscov));
0115 }
0116
0117 static inline int udp_lib_checksum_complete(struct sk_buff *skb)
0118 {
0119 return !skb_csum_unnecessary(skb) &&
0120 __udp_lib_checksum_complete(skb);
0121 }
0122
0123
0124
0125
0126
0127
0128
0129 static inline __wsum udp_csum_outgoing(struct sock *sk, struct sk_buff *skb)
0130 {
0131 __wsum csum = csum_partial(skb_transport_header(skb),
0132 sizeof(struct udphdr), 0);
0133 skb_queue_walk(&sk->sk_write_queue, skb) {
0134 csum = csum_add(csum, skb->csum);
0135 }
0136 return csum;
0137 }
0138
0139 static inline __wsum udp_csum(struct sk_buff *skb)
0140 {
0141 __wsum csum = csum_partial(skb_transport_header(skb),
0142 sizeof(struct udphdr), skb->csum);
0143
0144 for (skb = skb_shinfo(skb)->frag_list; skb; skb = skb->next) {
0145 csum = csum_add(csum, skb->csum);
0146 }
0147 return csum;
0148 }
0149
0150 static inline __sum16 udp_v4_check(int len, __be32 saddr,
0151 __be32 daddr, __wsum base)
0152 {
0153 return csum_tcpudp_magic(saddr, daddr, len, IPPROTO_UDP, base);
0154 }
0155
0156 void udp_set_csum(bool nocheck, struct sk_buff *skb,
0157 __be32 saddr, __be32 daddr, int len);
0158
0159 static inline void udp_csum_pull_header(struct sk_buff *skb)
0160 {
0161 if (!skb->csum_valid && skb->ip_summed == CHECKSUM_NONE)
0162 skb->csum = csum_partial(skb->data, sizeof(struct udphdr),
0163 skb->csum);
0164 skb_pull_rcsum(skb, sizeof(struct udphdr));
0165 UDP_SKB_CB(skb)->cscov -= sizeof(struct udphdr);
0166 }
0167
0168 typedef struct sock *(*udp_lookup_t)(const struct sk_buff *skb, __be16 sport,
0169 __be16 dport);
0170
0171 void udp_v6_early_demux(struct sk_buff *skb);
0172 INDIRECT_CALLABLE_DECLARE(int udpv6_rcv(struct sk_buff *));
0173
0174 struct sk_buff *__udp_gso_segment(struct sk_buff *gso_skb,
0175 netdev_features_t features, bool is_ipv6);
0176
0177
0178 static inline int udp_lib_hash(struct sock *sk)
0179 {
0180 BUG();
0181 return 0;
0182 }
0183
0184 void udp_lib_unhash(struct sock *sk);
0185 void udp_lib_rehash(struct sock *sk, u16 new_hash);
0186
0187 static inline void udp_lib_close(struct sock *sk, long timeout)
0188 {
0189 sk_common_release(sk);
0190 }
0191
0192 int udp_lib_get_port(struct sock *sk, unsigned short snum,
0193 unsigned int hash2_nulladdr);
0194
0195 u32 udp_flow_hashrnd(void);
0196
0197 static inline __be16 udp_flow_src_port(struct net *net, struct sk_buff *skb,
0198 int min, int max, bool use_eth)
0199 {
0200 u32 hash;
0201
0202 if (min >= max) {
0203
0204 inet_get_local_port_range(net, &min, &max);
0205 }
0206
0207 hash = skb_get_hash(skb);
0208 if (unlikely(!hash)) {
0209 if (use_eth) {
0210
0211
0212
0213 hash = jhash(skb->data, 2 * ETH_ALEN,
0214 (__force u32) skb->protocol);
0215 } else {
0216
0217
0218
0219 hash = udp_flow_hashrnd();
0220 }
0221 }
0222
0223
0224
0225
0226
0227
0228 hash ^= hash << 16;
0229
0230 return htons((((u64) hash * (max - min)) >> 32) + min);
0231 }
0232
0233 static inline int udp_rqueue_get(struct sock *sk)
0234 {
0235 return sk_rmem_alloc_get(sk) - READ_ONCE(udp_sk(sk)->forward_deficit);
0236 }
0237
0238 static inline bool udp_sk_bound_dev_eq(struct net *net, int bound_dev_if,
0239 int dif, int sdif)
0240 {
0241 #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
0242 return inet_bound_dev_eq(!!READ_ONCE(net->ipv4.sysctl_udp_l3mdev_accept),
0243 bound_dev_if, dif, sdif);
0244 #else
0245 return inet_bound_dev_eq(true, bound_dev_if, dif, sdif);
0246 #endif
0247 }
0248
0249
0250 void udp_destruct_sock(struct sock *sk);
0251 void skb_consume_udp(struct sock *sk, struct sk_buff *skb, int len);
0252 int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb);
0253 void udp_skb_destructor(struct sock *sk, struct sk_buff *skb);
0254 struct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags, int *off,
0255 int *err);
0256 static inline struct sk_buff *skb_recv_udp(struct sock *sk, unsigned int flags,
0257 int *err)
0258 {
0259 int off = 0;
0260
0261 return __skb_recv_udp(sk, flags, &off, err);
0262 }
0263
0264 int udp_v4_early_demux(struct sk_buff *skb);
0265 bool udp_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst);
0266 int udp_get_port(struct sock *sk, unsigned short snum,
0267 int (*saddr_cmp)(const struct sock *,
0268 const struct sock *));
0269 int udp_err(struct sk_buff *, u32);
0270 int udp_abort(struct sock *sk, int err);
0271 int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len);
0272 int udp_push_pending_frames(struct sock *sk);
0273 void udp_flush_pending_frames(struct sock *sk);
0274 int udp_cmsg_send(struct sock *sk, struct msghdr *msg, u16 *gso_size);
0275 void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst);
0276 int udp_rcv(struct sk_buff *skb);
0277 int udp_ioctl(struct sock *sk, int cmd, unsigned long arg);
0278 int udp_init_sock(struct sock *sk);
0279 int udp_pre_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
0280 int __udp_disconnect(struct sock *sk, int flags);
0281 int udp_disconnect(struct sock *sk, int flags);
0282 __poll_t udp_poll(struct file *file, struct socket *sock, poll_table *wait);
0283 struct sk_buff *skb_udp_tunnel_segment(struct sk_buff *skb,
0284 netdev_features_t features,
0285 bool is_ipv6);
0286 int udp_lib_getsockopt(struct sock *sk, int level, int optname,
0287 char __user *optval, int __user *optlen);
0288 int udp_lib_setsockopt(struct sock *sk, int level, int optname,
0289 sockptr_t optval, unsigned int optlen,
0290 int (*push_pending_frames)(struct sock *));
0291 struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport,
0292 __be32 daddr, __be16 dport, int dif);
0293 struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport,
0294 __be32 daddr, __be16 dport, int dif, int sdif,
0295 struct udp_table *tbl, struct sk_buff *skb);
0296 struct sock *udp4_lib_lookup_skb(const struct sk_buff *skb,
0297 __be16 sport, __be16 dport);
0298 struct sock *udp6_lib_lookup(struct net *net,
0299 const struct in6_addr *saddr, __be16 sport,
0300 const struct in6_addr *daddr, __be16 dport,
0301 int dif);
0302 struct sock *__udp6_lib_lookup(struct net *net,
0303 const struct in6_addr *saddr, __be16 sport,
0304 const struct in6_addr *daddr, __be16 dport,
0305 int dif, int sdif, struct udp_table *tbl,
0306 struct sk_buff *skb);
0307 struct sock *udp6_lib_lookup_skb(const struct sk_buff *skb,
0308 __be16 sport, __be16 dport);
0309 int udp_read_skb(struct sock *sk, skb_read_actor_t recv_actor);
0310
0311
0312
0313
0314 struct udp_dev_scratch {
0315
0316
0317
0318
0319 u32 _tsize_state;
0320
0321 #if BITS_PER_LONG == 64
0322
0323
0324
0325
0326
0327 u16 len;
0328 bool is_linear;
0329 bool csum_unnecessary;
0330 #endif
0331 };
0332
0333 static inline struct udp_dev_scratch *udp_skb_scratch(struct sk_buff *skb)
0334 {
0335 return (struct udp_dev_scratch *)&skb->dev_scratch;
0336 }
0337
0338 #if BITS_PER_LONG == 64
0339 static inline unsigned int udp_skb_len(struct sk_buff *skb)
0340 {
0341 return udp_skb_scratch(skb)->len;
0342 }
0343
0344 static inline bool udp_skb_csum_unnecessary(struct sk_buff *skb)
0345 {
0346 return udp_skb_scratch(skb)->csum_unnecessary;
0347 }
0348
0349 static inline bool udp_skb_is_linear(struct sk_buff *skb)
0350 {
0351 return udp_skb_scratch(skb)->is_linear;
0352 }
0353
0354 #else
0355 static inline unsigned int udp_skb_len(struct sk_buff *skb)
0356 {
0357 return skb->len;
0358 }
0359
0360 static inline bool udp_skb_csum_unnecessary(struct sk_buff *skb)
0361 {
0362 return skb_csum_unnecessary(skb);
0363 }
0364
0365 static inline bool udp_skb_is_linear(struct sk_buff *skb)
0366 {
0367 return !skb_is_nonlinear(skb);
0368 }
0369 #endif
0370
0371 static inline int copy_linear_skb(struct sk_buff *skb, int len, int off,
0372 struct iov_iter *to)
0373 {
0374 int n;
0375
0376 n = copy_to_iter(skb->data + off, len, to);
0377 if (n == len)
0378 return 0;
0379
0380 iov_iter_revert(to, n);
0381 return -EFAULT;
0382 }
0383
0384
0385
0386
0387 #define UDP_INC_STATS(net, field, is_udplite) do { \
0388 if (is_udplite) SNMP_INC_STATS((net)->mib.udplite_statistics, field); \
0389 else SNMP_INC_STATS((net)->mib.udp_statistics, field); } while(0)
0390 #define __UDP_INC_STATS(net, field, is_udplite) do { \
0391 if (is_udplite) __SNMP_INC_STATS((net)->mib.udplite_statistics, field); \
0392 else __SNMP_INC_STATS((net)->mib.udp_statistics, field); } while(0)
0393
0394 #define __UDP6_INC_STATS(net, field, is_udplite) do { \
0395 if (is_udplite) __SNMP_INC_STATS((net)->mib.udplite_stats_in6, field);\
0396 else __SNMP_INC_STATS((net)->mib.udp_stats_in6, field); \
0397 } while(0)
0398 #define UDP6_INC_STATS(net, field, __lite) do { \
0399 if (__lite) SNMP_INC_STATS((net)->mib.udplite_stats_in6, field); \
0400 else SNMP_INC_STATS((net)->mib.udp_stats_in6, field); \
0401 } while(0)
0402
0403 #if IS_ENABLED(CONFIG_IPV6)
0404 #define __UDPX_MIB(sk, ipv4) \
0405 ({ \
0406 ipv4 ? (IS_UDPLITE(sk) ? sock_net(sk)->mib.udplite_statistics : \
0407 sock_net(sk)->mib.udp_statistics) : \
0408 (IS_UDPLITE(sk) ? sock_net(sk)->mib.udplite_stats_in6 : \
0409 sock_net(sk)->mib.udp_stats_in6); \
0410 })
0411 #else
0412 #define __UDPX_MIB(sk, ipv4) \
0413 ({ \
0414 IS_UDPLITE(sk) ? sock_net(sk)->mib.udplite_statistics : \
0415 sock_net(sk)->mib.udp_statistics; \
0416 })
0417 #endif
0418
0419 #define __UDPX_INC_STATS(sk, field) \
0420 __SNMP_INC_STATS(__UDPX_MIB(sk, (sk)->sk_family == AF_INET), field)
0421
0422 #ifdef CONFIG_PROC_FS
0423 struct udp_seq_afinfo {
0424 sa_family_t family;
0425 struct udp_table *udp_table;
0426 };
0427
0428 struct udp_iter_state {
0429 struct seq_net_private p;
0430 int bucket;
0431 struct udp_seq_afinfo *bpf_seq_afinfo;
0432 };
0433
0434 void *udp_seq_start(struct seq_file *seq, loff_t *pos);
0435 void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos);
0436 void udp_seq_stop(struct seq_file *seq, void *v);
0437
0438 extern const struct seq_operations udp_seq_ops;
0439 extern const struct seq_operations udp6_seq_ops;
0440
0441 int udp4_proc_init(void);
0442 void udp4_proc_exit(void);
0443 #endif
0444
0445 int udpv4_offload_init(void);
0446
0447 void udp_init(void);
0448
0449 DECLARE_STATIC_KEY_FALSE(udp_encap_needed_key);
0450 void udp_encap_enable(void);
0451 void udp_encap_disable(void);
0452 #if IS_ENABLED(CONFIG_IPV6)
0453 DECLARE_STATIC_KEY_FALSE(udpv6_encap_needed_key);
0454 void udpv6_encap_enable(void);
0455 #endif
0456
0457 static inline struct sk_buff *udp_rcv_segment(struct sock *sk,
0458 struct sk_buff *skb, bool ipv4)
0459 {
0460 netdev_features_t features = NETIF_F_SG;
0461 struct sk_buff *segs;
0462
0463
0464
0465
0466 if (!inet_get_convert_csum(sk))
0467 features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
0468
0469
0470
0471
0472
0473
0474
0475
0476
0477 if (skb->pkt_type == PACKET_LOOPBACK)
0478 skb->ip_summed = CHECKSUM_PARTIAL;
0479
0480
0481
0482
0483 segs = __skb_gso_segment(skb, features, false);
0484 if (IS_ERR_OR_NULL(segs)) {
0485 int segs_nr = skb_shinfo(skb)->gso_segs;
0486
0487 atomic_add(segs_nr, &sk->sk_drops);
0488 SNMP_ADD_STATS(__UDPX_MIB(sk, ipv4), UDP_MIB_INERRORS, segs_nr);
0489 kfree_skb(skb);
0490 return NULL;
0491 }
0492
0493 consume_skb(skb);
0494 return segs;
0495 }
0496
0497 static inline void udp_post_segment_fix_csum(struct sk_buff *skb)
0498 {
0499
0500 WARN_ON_ONCE(UDP_SKB_CB(skb)->partial_cov);
0501
0502
0503
0504
0505
0506
0507
0508
0509
0510
0511
0512
0513
0514
0515 UDP_SKB_CB(skb)->cscov = skb->len;
0516 if (skb->ip_summed == CHECKSUM_NONE && !skb->csum_valid)
0517 skb->csum_valid = 1;
0518 }
0519
0520 #ifdef CONFIG_BPF_SYSCALL
0521 struct sk_psock;
0522 struct proto *udp_bpf_get_proto(struct sock *sk, struct sk_psock *psock);
0523 int udp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore);
0524 #endif
0525
0526 #endif