1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * PFCP according to 3GPP TS 29.244 4 * 5 * Copyright (C) 2022, Intel Corporation. 6 */ 7 8 #include <linux/module.h> 9 #include <linux/netdevice.h> 10 #include <linux/rculist.h> 11 #include <linux/skbuff.h> 12 #include <linux/types.h> 13 14 #include <net/udp.h> 15 #include <net/udp_tunnel.h> 16 #include <net/pfcp.h> 17 18 struct pfcp_dev { 19 struct list_head list; 20 21 struct socket *sock; 22 struct net_device *dev; 23 struct net *net; 24 25 struct gro_cells gro_cells; 26 }; 27 28 static unsigned int pfcp_net_id __read_mostly; 29 30 struct pfcp_net { 31 struct list_head pfcp_dev_list; 32 }; 33 34 static void 35 pfcp_session_recv(struct pfcp_dev *pfcp, struct sk_buff *skb, 36 struct pfcp_metadata *md) 37 { 38 struct pfcphdr_session *unparsed = pfcp_hdr_session(skb); 39 40 md->seid = unparsed->seid; 41 md->type = PFCP_TYPE_SESSION; 42 } 43 44 static void 45 pfcp_node_recv(struct pfcp_dev *pfcp, struct sk_buff *skb, 46 struct pfcp_metadata *md) 47 { 48 md->type = PFCP_TYPE_NODE; 49 } 50 51 static int pfcp_encap_recv(struct sock *sk, struct sk_buff *skb) 52 { 53 IP_TUNNEL_DECLARE_FLAGS(flags) = { }; 54 struct metadata_dst *tun_dst; 55 struct pfcp_metadata *md; 56 struct pfcphdr *unparsed; 57 struct pfcp_dev *pfcp; 58 59 if (unlikely(!pskb_may_pull(skb, PFCP_HLEN))) 60 goto drop; 61 62 pfcp = rcu_dereference_sk_user_data(sk); 63 if (unlikely(!pfcp)) 64 goto drop; 65 66 unparsed = pfcp_hdr(skb); 67 68 ip_tunnel_flags_zero(flags); 69 tun_dst = udp_tun_rx_dst(skb, sk->sk_family, flags, 0, 70 sizeof(*md)); 71 if (unlikely(!tun_dst)) 72 goto drop; 73 74 md = ip_tunnel_info_opts(&tun_dst->u.tun_info); 75 if (unlikely(!md)) 76 goto drop; 77 78 if (unparsed->flags & PFCP_SEID_FLAG) 79 pfcp_session_recv(pfcp, skb, md); 80 else 81 pfcp_node_recv(pfcp, skb, md); 82 83 __set_bit(IP_TUNNEL_PFCP_OPT_BIT, flags); 84 ip_tunnel_info_opts_set(&tun_dst->u.tun_info, md, sizeof(*md), 85 flags); 86 87 if (unlikely(iptunnel_pull_header(skb, PFCP_HLEN, skb->protocol, 88 !net_eq(sock_net(sk), 89 dev_net(pfcp->dev))))) 90 goto drop; 91 92 skb_dst_set(skb, (struct dst_entry *)tun_dst); 93 94 skb_reset_network_header(skb); 95 skb_reset_mac_header(skb); 96 skb->dev = pfcp->dev; 97 98 gro_cells_receive(&pfcp->gro_cells, skb); 99 100 return 0; 101 drop: 102 kfree_skb(skb); 103 return 0; 104 } 105 106 static void pfcp_del_sock(struct pfcp_dev *pfcp) 107 { 108 udp_tunnel_sock_release(pfcp->sock); 109 pfcp->sock = NULL; 110 } 111 112 static void pfcp_dev_uninit(struct net_device *dev) 113 { 114 struct pfcp_dev *pfcp = netdev_priv(dev); 115 116 gro_cells_destroy(&pfcp->gro_cells); 117 pfcp_del_sock(pfcp); 118 } 119 120 static int pfcp_dev_init(struct net_device *dev) 121 { 122 struct pfcp_dev *pfcp = netdev_priv(dev); 123 124 pfcp->dev = dev; 125 126 return gro_cells_init(&pfcp->gro_cells, dev); 127 } 128 129 static const struct net_device_ops pfcp_netdev_ops = { 130 .ndo_init = pfcp_dev_init, 131 .ndo_uninit = pfcp_dev_uninit, 132 .ndo_get_stats64 = dev_get_tstats64, 133 }; 134 135 static const struct device_type pfcp_type = { 136 .name = "pfcp", 137 }; 138 139 static void pfcp_link_setup(struct net_device *dev) 140 { 141 dev->netdev_ops = &pfcp_netdev_ops; 142 dev->needs_free_netdev = true; 143 SET_NETDEV_DEVTYPE(dev, &pfcp_type); 144 145 dev->hard_header_len = 0; 146 dev->addr_len = 0; 147 148 dev->type = ARPHRD_NONE; 149 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; 150 dev->priv_flags |= IFF_NO_QUEUE; 151 152 netif_keep_dst(dev); 153 } 154 155 static struct socket *pfcp_create_sock(struct pfcp_dev *pfcp) 156 { 157 struct udp_tunnel_sock_cfg tuncfg = {}; 158 struct udp_port_cfg udp_conf = { 159 .local_ip.s_addr = htonl(INADDR_ANY), 160 .family = AF_INET, 161 }; 162 struct net *net = pfcp->net; 163 struct socket *sock; 164 int err; 165 166 udp_conf.local_udp_port = htons(PFCP_PORT); 167 168 err = udp_sock_create(net, &udp_conf, &sock); 169 if (err) 170 return ERR_PTR(err); 171 172 tuncfg.sk_user_data = pfcp; 173 tuncfg.encap_rcv = pfcp_encap_recv; 174 tuncfg.encap_type = 1; 175 176 setup_udp_tunnel_sock(net, sock, &tuncfg); 177 178 return sock; 179 } 180 181 static int pfcp_add_sock(struct pfcp_dev *pfcp) 182 { 183 pfcp->sock = pfcp_create_sock(pfcp); 184 185 return PTR_ERR_OR_ZERO(pfcp->sock); 186 } 187 188 static int pfcp_newlink(struct net *net, struct net_device *dev, 189 struct nlattr *tb[], struct nlattr *data[], 190 struct netlink_ext_ack *extack) 191 { 192 struct pfcp_dev *pfcp = netdev_priv(dev); 193 struct pfcp_net *pn; 194 int err; 195 196 pfcp->net = net; 197 198 err = pfcp_add_sock(pfcp); 199 if (err) { 200 netdev_dbg(dev, "failed to add pfcp socket %d\n", err); 201 goto exit_err; 202 } 203 204 err = register_netdevice(dev); 205 if (err) { 206 netdev_dbg(dev, "failed to register pfcp netdev %d\n", err); 207 goto exit_del_pfcp_sock; 208 } 209 210 pn = net_generic(dev_net(dev), pfcp_net_id); 211 list_add_rcu(&pfcp->list, &pn->pfcp_dev_list); 212 213 netdev_dbg(dev, "registered new PFCP interface\n"); 214 215 return 0; 216 217 exit_del_pfcp_sock: 218 pfcp_del_sock(pfcp); 219 exit_err: 220 pfcp->net = NULL; 221 return err; 222 } 223 224 static void pfcp_dellink(struct net_device *dev, struct list_head *head) 225 { 226 struct pfcp_dev *pfcp = netdev_priv(dev); 227 228 list_del_rcu(&pfcp->list); 229 unregister_netdevice_queue(dev, head); 230 } 231 232 static struct rtnl_link_ops pfcp_link_ops __read_mostly = { 233 .kind = "pfcp", 234 .priv_size = sizeof(struct pfcp_dev), 235 .setup = pfcp_link_setup, 236 .newlink = pfcp_newlink, 237 .dellink = pfcp_dellink, 238 }; 239 240 static int __net_init pfcp_net_init(struct net *net) 241 { 242 struct pfcp_net *pn = net_generic(net, pfcp_net_id); 243 244 INIT_LIST_HEAD(&pn->pfcp_dev_list); 245 return 0; 246 } 247 248 static void __net_exit pfcp_net_exit(struct net *net) 249 { 250 struct pfcp_net *pn = net_generic(net, pfcp_net_id); 251 struct pfcp_dev *pfcp; 252 LIST_HEAD(list); 253 254 rtnl_lock(); 255 list_for_each_entry(pfcp, &pn->pfcp_dev_list, list) 256 pfcp_dellink(pfcp->dev, &list); 257 258 unregister_netdevice_many(&list); 259 rtnl_unlock(); 260 } 261 262 static struct pernet_operations pfcp_net_ops = { 263 .init = pfcp_net_init, 264 .exit = pfcp_net_exit, 265 .id = &pfcp_net_id, 266 .size = sizeof(struct pfcp_net), 267 }; 268 269 static int __init pfcp_init(void) 270 { 271 int err; 272 273 err = register_pernet_subsys(&pfcp_net_ops); 274 if (err) 275 goto exit_err; 276 277 err = rtnl_link_register(&pfcp_link_ops); 278 if (err) 279 goto exit_unregister_subsys; 280 return 0; 281 282 exit_unregister_subsys: 283 unregister_pernet_subsys(&pfcp_net_ops); 284 exit_err: 285 pr_err("loading PFCP module failed: err %d\n", err); 286 return err; 287 } 288 late_initcall(pfcp_init); 289 290 static void __exit pfcp_exit(void) 291 { 292 rtnl_link_unregister(&pfcp_link_ops); 293 unregister_pernet_subsys(&pfcp_net_ops); 294 295 pr_info("PFCP module unloaded\n"); 296 } 297 module_exit(pfcp_exit); 298 299 MODULE_LICENSE("GPL"); 300 MODULE_AUTHOR("Wojciech Drewek <wojciech.drewek@intel.com>"); 301 MODULE_DESCRIPTION("Interface driver for PFCP encapsulated traffic"); 302 MODULE_ALIAS_RTNL_LINK("pfcp"); 303