1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * UDPLITE An implementation of the UDP-Lite protocol (RFC 3828).
4 *
5 * Authors: Gerrit Renker <gerrit@erg.abdn.ac.uk>
6 *
7 * Changes:
8 * Fixes:
9 */
10
11 #define pr_fmt(fmt) "UDPLite: " fmt
12
13 #include <linux/export.h>
14 #include <linux/proc_fs.h>
15 #include "udp_impl.h"
16
17 struct udp_table udplite_table __read_mostly;
18 EXPORT_SYMBOL(udplite_table);
19
20 /* Designate sk as UDP-Lite socket */
udplite_sk_init(struct sock * sk)21 static int udplite_sk_init(struct sock *sk)
22 {
23 udp_init_sock(sk);
24 udp_sk(sk)->pcflag = UDPLITE_BIT;
25 return 0;
26 }
27
udplite_rcv(struct sk_buff * skb)28 static int udplite_rcv(struct sk_buff *skb)
29 {
30 return __udp4_lib_rcv(skb, &udplite_table, IPPROTO_UDPLITE);
31 }
32
udplite_err(struct sk_buff * skb,u32 info)33 static int udplite_err(struct sk_buff *skb, u32 info)
34 {
35 return __udp4_lib_err(skb, info, &udplite_table);
36 }
37
38 static const struct net_protocol udplite_protocol = {
39 .handler = udplite_rcv,
40 .err_handler = udplite_err,
41 .no_policy = 1,
42 };
43
44 struct proto udplite_prot = {
45 .name = "UDP-Lite",
46 .owner = THIS_MODULE,
47 .close = udp_lib_close,
48 .connect = ip4_datagram_connect,
49 .disconnect = udp_disconnect,
50 .ioctl = udp_ioctl,
51 .init = udplite_sk_init,
52 .destroy = udp_destroy_sock,
53 .setsockopt = udp_setsockopt,
54 .getsockopt = udp_getsockopt,
55 .sendmsg = udp_sendmsg,
56 .recvmsg = udp_recvmsg,
57 .sendpage = udp_sendpage,
58 .hash = udp_lib_hash,
59 .unhash = udp_lib_unhash,
60 .rehash = udp_v4_rehash,
61 .get_port = udp_v4_get_port,
62 .memory_allocated = &udp_memory_allocated,
63 .sysctl_mem = sysctl_udp_mem,
64 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_udp_wmem_min),
65 .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_udp_rmem_min),
66 .obj_size = sizeof(struct udp_sock),
67 .h.udp_table = &udplite_table,
68 };
69 EXPORT_SYMBOL(udplite_prot);
70
71 static struct inet_protosw udplite4_protosw = {
72 .type = SOCK_DGRAM,
73 .protocol = IPPROTO_UDPLITE,
74 .prot = &udplite_prot,
75 .ops = &inet_dgram_ops,
76 .flags = INET_PROTOSW_PERMANENT,
77 };
78
79 #ifdef CONFIG_PROC_FS
80 static struct udp_seq_afinfo udplite4_seq_afinfo = {
81 .family = AF_INET,
82 .udp_table = &udplite_table,
83 };
84
udplite4_proc_init_net(struct net * net)85 static int __net_init udplite4_proc_init_net(struct net *net)
86 {
87 if (!proc_create_net_data("udplite", 0444, net->proc_net, &udp_seq_ops,
88 sizeof(struct udp_iter_state), &udplite4_seq_afinfo))
89 return -ENOMEM;
90 return 0;
91 }
92
udplite4_proc_exit_net(struct net * net)93 static void __net_exit udplite4_proc_exit_net(struct net *net)
94 {
95 remove_proc_entry("udplite", net->proc_net);
96 }
97
98 static struct pernet_operations udplite4_net_ops = {
99 .init = udplite4_proc_init_net,
100 .exit = udplite4_proc_exit_net,
101 };
102
udplite4_proc_init(void)103 static __init int udplite4_proc_init(void)
104 {
105 return register_pernet_subsys(&udplite4_net_ops);
106 }
107 #else
udplite4_proc_init(void)108 static inline int udplite4_proc_init(void)
109 {
110 return 0;
111 }
112 #endif
113
udplite4_register(void)114 void __init udplite4_register(void)
115 {
116 udp_table_init(&udplite_table, "UDP-Lite");
117 if (proto_register(&udplite_prot, 1))
118 goto out_register_err;
119
120 if (inet_add_protocol(&udplite_protocol, IPPROTO_UDPLITE) < 0)
121 goto out_unregister_proto;
122
123 inet_register_protosw(&udplite4_protosw);
124
125 if (udplite4_proc_init())
126 pr_err("%s: Cannot register /proc!\n", __func__);
127 return;
128
129 out_unregister_proto:
130 proto_unregister(&udplite_prot);
131 out_register_err:
132 pr_crit("%s: Cannot add UDP-Lite protocol\n", __func__);
133 }
134