1 /*
2  * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
3  * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *	- Redistributions of source code must retain the above
16  *	  copyright notice, this list of conditions and the following
17  *	  disclaimer.
18  *
19  *	- Redistributions in binary form must reproduce the above
20  *	  copyright notice, this list of conditions and the following
21  *	  disclaimer in the documentation and/or other materials
22  *	  provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 
34 #include <linux/skbuff.h>
35 #include <linux/if_arp.h>
36 #include <linux/netdevice.h>
37 #include <linux/if.h>
38 #include <linux/if_vlan.h>
39 #include <net/udp_tunnel.h>
40 #include <net/sch_generic.h>
41 #include <linux/netfilter.h>
42 #include <rdma/ib_addr.h>
43 
44 #include "rxe.h"
45 #include "rxe_net.h"
46 #include "rxe_loc.h"
47 
48 static LIST_HEAD(rxe_dev_list);
49 static DEFINE_SPINLOCK(dev_list_lock); /* spinlock for device list */
50 
51 struct rxe_dev *net_to_rxe(struct net_device *ndev)
52 {
53 	struct rxe_dev *rxe;
54 	struct rxe_dev *found = NULL;
55 
56 	spin_lock_bh(&dev_list_lock);
57 	list_for_each_entry(rxe, &rxe_dev_list, list) {
58 		if (rxe->ndev == ndev) {
59 			found = rxe;
60 			break;
61 		}
62 	}
63 	spin_unlock_bh(&dev_list_lock);
64 
65 	return found;
66 }
67 
68 struct rxe_dev *get_rxe_by_name(const char *name)
69 {
70 	struct rxe_dev *rxe;
71 	struct rxe_dev *found = NULL;
72 
73 	spin_lock_bh(&dev_list_lock);
74 	list_for_each_entry(rxe, &rxe_dev_list, list) {
75 		if (!strcmp(name, dev_name(&rxe->ib_dev.dev))) {
76 			found = rxe;
77 			break;
78 		}
79 	}
80 	spin_unlock_bh(&dev_list_lock);
81 	return found;
82 }
83 
84 
85 static struct rxe_recv_sockets recv_sockets;
86 
87 struct device *rxe_dma_device(struct rxe_dev *rxe)
88 {
89 	struct net_device *ndev;
90 
91 	ndev = rxe->ndev;
92 
93 	if (is_vlan_dev(ndev))
94 		ndev = vlan_dev_real_dev(ndev);
95 
96 	return ndev->dev.parent;
97 }
98 
99 int rxe_mcast_add(struct rxe_dev *rxe, union ib_gid *mgid)
100 {
101 	int err;
102 	unsigned char ll_addr[ETH_ALEN];
103 
104 	ipv6_eth_mc_map((struct in6_addr *)mgid->raw, ll_addr);
105 	err = dev_mc_add(rxe->ndev, ll_addr);
106 
107 	return err;
108 }
109 
110 int rxe_mcast_delete(struct rxe_dev *rxe, union ib_gid *mgid)
111 {
112 	int err;
113 	unsigned char ll_addr[ETH_ALEN];
114 
115 	ipv6_eth_mc_map((struct in6_addr *)mgid->raw, ll_addr);
116 	err = dev_mc_del(rxe->ndev, ll_addr);
117 
118 	return err;
119 }
120 
121 static struct dst_entry *rxe_find_route4(struct net_device *ndev,
122 				  struct in_addr *saddr,
123 				  struct in_addr *daddr)
124 {
125 	struct rtable *rt;
126 	struct flowi4 fl = { { 0 } };
127 
128 	memset(&fl, 0, sizeof(fl));
129 	fl.flowi4_oif = ndev->ifindex;
130 	memcpy(&fl.saddr, saddr, sizeof(*saddr));
131 	memcpy(&fl.daddr, daddr, sizeof(*daddr));
132 	fl.flowi4_proto = IPPROTO_UDP;
133 
134 	rt = ip_route_output_key(&init_net, &fl);
135 	if (IS_ERR(rt)) {
136 		pr_err_ratelimited("no route to %pI4\n", &daddr->s_addr);
137 		return NULL;
138 	}
139 
140 	return &rt->dst;
141 }
142 
143 #if IS_ENABLED(CONFIG_IPV6)
144 static struct dst_entry *rxe_find_route6(struct net_device *ndev,
145 					 struct in6_addr *saddr,
146 					 struct in6_addr *daddr)
147 {
148 	struct dst_entry *ndst;
149 	struct flowi6 fl6 = { { 0 } };
150 
151 	memset(&fl6, 0, sizeof(fl6));
152 	fl6.flowi6_oif = ndev->ifindex;
153 	memcpy(&fl6.saddr, saddr, sizeof(*saddr));
154 	memcpy(&fl6.daddr, daddr, sizeof(*daddr));
155 	fl6.flowi6_proto = IPPROTO_UDP;
156 
157 	if (unlikely(ipv6_stub->ipv6_dst_lookup(sock_net(recv_sockets.sk6->sk),
158 						recv_sockets.sk6->sk, &ndst, &fl6))) {
159 		pr_err_ratelimited("no route to %pI6\n", daddr);
160 		goto put;
161 	}
162 
163 	if (unlikely(ndst->error)) {
164 		pr_err("no route to %pI6\n", daddr);
165 		goto put;
166 	}
167 
168 	return ndst;
169 put:
170 	dst_release(ndst);
171 	return NULL;
172 }
173 
174 #else
175 
176 static struct dst_entry *rxe_find_route6(struct net_device *ndev,
177 					 struct in6_addr *saddr,
178 					 struct in6_addr *daddr)
179 {
180 	return NULL;
181 }
182 
183 #endif
184 
185 static struct dst_entry *rxe_find_route(struct net_device *ndev,
186 					struct rxe_qp *qp,
187 					struct rxe_av *av)
188 {
189 	struct dst_entry *dst = NULL;
190 
191 	if (qp_type(qp) == IB_QPT_RC)
192 		dst = sk_dst_get(qp->sk->sk);
193 
194 	if (!dst || !dst_check(dst, qp->dst_cookie)) {
195 		if (dst)
196 			dst_release(dst);
197 
198 		if (av->network_type == RDMA_NETWORK_IPV4) {
199 			struct in_addr *saddr;
200 			struct in_addr *daddr;
201 
202 			saddr = &av->sgid_addr._sockaddr_in.sin_addr;
203 			daddr = &av->dgid_addr._sockaddr_in.sin_addr;
204 			dst = rxe_find_route4(ndev, saddr, daddr);
205 		} else if (av->network_type == RDMA_NETWORK_IPV6) {
206 			struct in6_addr *saddr6;
207 			struct in6_addr *daddr6;
208 
209 			saddr6 = &av->sgid_addr._sockaddr_in6.sin6_addr;
210 			daddr6 = &av->dgid_addr._sockaddr_in6.sin6_addr;
211 			dst = rxe_find_route6(ndev, saddr6, daddr6);
212 #if IS_ENABLED(CONFIG_IPV6)
213 			if (dst)
214 				qp->dst_cookie =
215 					rt6_get_cookie((struct rt6_info *)dst);
216 #endif
217 		}
218 
219 		if (dst && (qp_type(qp) == IB_QPT_RC)) {
220 			dst_hold(dst);
221 			sk_dst_set(qp->sk->sk, dst);
222 		}
223 	}
224 	return dst;
225 }
226 
227 static int rxe_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
228 {
229 	struct udphdr *udph;
230 	struct net_device *ndev = skb->dev;
231 	struct net_device *rdev = ndev;
232 	struct rxe_dev *rxe = net_to_rxe(ndev);
233 	struct rxe_pkt_info *pkt = SKB_TO_PKT(skb);
234 
235 	if (!rxe && is_vlan_dev(rdev)) {
236 		rdev = vlan_dev_real_dev(ndev);
237 		rxe = net_to_rxe(rdev);
238 	}
239 	if (!rxe)
240 		goto drop;
241 
242 	if (skb_linearize(skb)) {
243 		pr_err("skb_linearize failed\n");
244 		goto drop;
245 	}
246 
247 	udph = udp_hdr(skb);
248 	pkt->rxe = rxe;
249 	pkt->port_num = 1;
250 	pkt->hdr = (u8 *)(udph + 1);
251 	pkt->mask = RXE_GRH_MASK;
252 	pkt->paylen = be16_to_cpu(udph->len) - sizeof(*udph);
253 
254 	rxe_rcv(skb);
255 
256 	return 0;
257 drop:
258 	kfree_skb(skb);
259 
260 	return 0;
261 }
262 
263 static struct socket *rxe_setup_udp_tunnel(struct net *net, __be16 port,
264 					   bool ipv6)
265 {
266 	int err;
267 	struct socket *sock;
268 	struct udp_port_cfg udp_cfg = { };
269 	struct udp_tunnel_sock_cfg tnl_cfg = { };
270 
271 	if (ipv6) {
272 		udp_cfg.family = AF_INET6;
273 		udp_cfg.ipv6_v6only = 1;
274 	} else {
275 		udp_cfg.family = AF_INET;
276 	}
277 
278 	udp_cfg.local_udp_port = port;
279 
280 	/* Create UDP socket */
281 	err = udp_sock_create(net, &udp_cfg, &sock);
282 	if (err < 0) {
283 		pr_err("failed to create udp socket. err = %d\n", err);
284 		return ERR_PTR(err);
285 	}
286 
287 	tnl_cfg.encap_type = 1;
288 	tnl_cfg.encap_rcv = rxe_udp_encap_recv;
289 
290 	/* Setup UDP tunnel */
291 	setup_udp_tunnel_sock(net, sock, &tnl_cfg);
292 
293 	return sock;
294 }
295 
296 static void rxe_release_udp_tunnel(struct socket *sk)
297 {
298 	if (sk)
299 		udp_tunnel_sock_release(sk);
300 }
301 
302 static void prepare_udp_hdr(struct sk_buff *skb, __be16 src_port,
303 			    __be16 dst_port)
304 {
305 	struct udphdr *udph;
306 
307 	__skb_push(skb, sizeof(*udph));
308 	skb_reset_transport_header(skb);
309 	udph = udp_hdr(skb);
310 
311 	udph->dest = dst_port;
312 	udph->source = src_port;
313 	udph->len = htons(skb->len);
314 	udph->check = 0;
315 }
316 
317 static void prepare_ipv4_hdr(struct dst_entry *dst, struct sk_buff *skb,
318 			     __be32 saddr, __be32 daddr, __u8 proto,
319 			     __u8 tos, __u8 ttl, __be16 df, bool xnet)
320 {
321 	struct iphdr *iph;
322 
323 	skb_scrub_packet(skb, xnet);
324 
325 	skb_clear_hash(skb);
326 	skb_dst_set(skb, dst_clone(dst));
327 	memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
328 
329 	skb_push(skb, sizeof(struct iphdr));
330 	skb_reset_network_header(skb);
331 
332 	iph = ip_hdr(skb);
333 
334 	iph->version	=	IPVERSION;
335 	iph->ihl	=	sizeof(struct iphdr) >> 2;
336 	iph->frag_off	=	df;
337 	iph->protocol	=	proto;
338 	iph->tos	=	tos;
339 	iph->daddr	=	daddr;
340 	iph->saddr	=	saddr;
341 	iph->ttl	=	ttl;
342 	__ip_select_ident(dev_net(dst->dev), iph,
343 			  skb_shinfo(skb)->gso_segs ?: 1);
344 	iph->tot_len = htons(skb->len);
345 	ip_send_check(iph);
346 }
347 
348 static void prepare_ipv6_hdr(struct dst_entry *dst, struct sk_buff *skb,
349 			     struct in6_addr *saddr, struct in6_addr *daddr,
350 			     __u8 proto, __u8 prio, __u8 ttl)
351 {
352 	struct ipv6hdr *ip6h;
353 
354 	memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
355 	IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED
356 			    | IPSKB_REROUTED);
357 	skb_dst_set(skb, dst_clone(dst));
358 
359 	__skb_push(skb, sizeof(*ip6h));
360 	skb_reset_network_header(skb);
361 	ip6h		  = ipv6_hdr(skb);
362 	ip6_flow_hdr(ip6h, prio, htonl(0));
363 	ip6h->payload_len = htons(skb->len);
364 	ip6h->nexthdr     = proto;
365 	ip6h->hop_limit   = ttl;
366 	ip6h->daddr	  = *daddr;
367 	ip6h->saddr	  = *saddr;
368 	ip6h->payload_len = htons(skb->len - sizeof(*ip6h));
369 }
370 
371 static int prepare4(struct rxe_pkt_info *pkt, struct sk_buff *skb,
372 		    struct rxe_av *av)
373 {
374 	struct rxe_qp *qp = pkt->qp;
375 	struct dst_entry *dst;
376 	bool xnet = false;
377 	__be16 df = htons(IP_DF);
378 	struct in_addr *saddr = &av->sgid_addr._sockaddr_in.sin_addr;
379 	struct in_addr *daddr = &av->dgid_addr._sockaddr_in.sin_addr;
380 
381 	dst = rxe_find_route(skb->dev, qp, av);
382 	if (!dst) {
383 		pr_err("Host not reachable\n");
384 		return -EHOSTUNREACH;
385 	}
386 
387 	if (!memcmp(saddr, daddr, sizeof(*daddr)))
388 		pkt->mask |= RXE_LOOPBACK_MASK;
389 
390 	prepare_udp_hdr(skb, cpu_to_be16(qp->src_port),
391 			cpu_to_be16(ROCE_V2_UDP_DPORT));
392 
393 	prepare_ipv4_hdr(dst, skb, saddr->s_addr, daddr->s_addr, IPPROTO_UDP,
394 			 av->grh.traffic_class, av->grh.hop_limit, df, xnet);
395 
396 	dst_release(dst);
397 	return 0;
398 }
399 
400 static int prepare6(struct rxe_pkt_info *pkt, struct sk_buff *skb,
401 		    struct rxe_av *av)
402 {
403 	struct rxe_qp *qp = pkt->qp;
404 	struct dst_entry *dst;
405 	struct in6_addr *saddr = &av->sgid_addr._sockaddr_in6.sin6_addr;
406 	struct in6_addr *daddr = &av->dgid_addr._sockaddr_in6.sin6_addr;
407 
408 	dst = rxe_find_route(skb->dev, qp, av);
409 	if (!dst) {
410 		pr_err("Host not reachable\n");
411 		return -EHOSTUNREACH;
412 	}
413 
414 	if (!memcmp(saddr, daddr, sizeof(*daddr)))
415 		pkt->mask |= RXE_LOOPBACK_MASK;
416 
417 	prepare_udp_hdr(skb, cpu_to_be16(qp->src_port),
418 			cpu_to_be16(ROCE_V2_UDP_DPORT));
419 
420 	prepare_ipv6_hdr(dst, skb, saddr, daddr, IPPROTO_UDP,
421 			 av->grh.traffic_class,
422 			 av->grh.hop_limit);
423 
424 	dst_release(dst);
425 	return 0;
426 }
427 
428 int rxe_prepare(struct rxe_pkt_info *pkt, struct sk_buff *skb, u32 *crc)
429 {
430 	int err = 0;
431 	struct rxe_av *av = rxe_get_av(pkt);
432 
433 	if (av->network_type == RDMA_NETWORK_IPV4)
434 		err = prepare4(pkt, skb, av);
435 	else if (av->network_type == RDMA_NETWORK_IPV6)
436 		err = prepare6(pkt, skb, av);
437 
438 	*crc = rxe_icrc_hdr(pkt, skb);
439 
440 	return err;
441 }
442 
443 static void rxe_skb_tx_dtor(struct sk_buff *skb)
444 {
445 	struct sock *sk = skb->sk;
446 	struct rxe_qp *qp = sk->sk_user_data;
447 	int skb_out = atomic_dec_return(&qp->skb_out);
448 
449 	if (unlikely(qp->need_req_skb &&
450 		     skb_out < RXE_INFLIGHT_SKBS_PER_QP_LOW))
451 		rxe_run_task(&qp->req.task, 1);
452 
453 	rxe_drop_ref(qp);
454 }
455 
456 int rxe_send(struct rxe_pkt_info *pkt, struct sk_buff *skb)
457 {
458 	struct rxe_av *av;
459 	int err;
460 
461 	av = rxe_get_av(pkt);
462 
463 	skb->destructor = rxe_skb_tx_dtor;
464 	skb->sk = pkt->qp->sk->sk;
465 
466 	rxe_add_ref(pkt->qp);
467 	atomic_inc(&pkt->qp->skb_out);
468 
469 	if (av->network_type == RDMA_NETWORK_IPV4) {
470 		err = ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
471 	} else if (av->network_type == RDMA_NETWORK_IPV6) {
472 		err = ip6_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
473 	} else {
474 		pr_err("Unknown layer 3 protocol: %d\n", av->network_type);
475 		atomic_dec(&pkt->qp->skb_out);
476 		rxe_drop_ref(pkt->qp);
477 		kfree_skb(skb);
478 		return -EINVAL;
479 	}
480 
481 	if (unlikely(net_xmit_eval(err))) {
482 		pr_debug("error sending packet: %d\n", err);
483 		return -EAGAIN;
484 	}
485 
486 	return 0;
487 }
488 
489 void rxe_loopback(struct sk_buff *skb)
490 {
491 	rxe_rcv(skb);
492 }
493 
494 struct sk_buff *rxe_init_packet(struct rxe_dev *rxe, struct rxe_av *av,
495 				int paylen, struct rxe_pkt_info *pkt)
496 {
497 	unsigned int hdr_len;
498 	struct sk_buff *skb;
499 	struct net_device *ndev;
500 	const struct ib_gid_attr *attr;
501 	const int port_num = 1;
502 
503 	attr = rdma_get_gid_attr(&rxe->ib_dev, port_num, av->grh.sgid_index);
504 	if (IS_ERR(attr))
505 		return NULL;
506 	ndev = attr->ndev;
507 
508 	if (av->network_type == RDMA_NETWORK_IPV4)
509 		hdr_len = ETH_HLEN + sizeof(struct udphdr) +
510 			sizeof(struct iphdr);
511 	else
512 		hdr_len = ETH_HLEN + sizeof(struct udphdr) +
513 			sizeof(struct ipv6hdr);
514 
515 	skb = alloc_skb(paylen + hdr_len + LL_RESERVED_SPACE(ndev),
516 			GFP_ATOMIC);
517 
518 	if (unlikely(!skb))
519 		goto out;
520 
521 	skb_reserve(skb, hdr_len + LL_RESERVED_SPACE(rxe->ndev));
522 
523 	skb->dev	= ndev;
524 	if (av->network_type == RDMA_NETWORK_IPV4)
525 		skb->protocol = htons(ETH_P_IP);
526 	else
527 		skb->protocol = htons(ETH_P_IPV6);
528 
529 	pkt->rxe	= rxe;
530 	pkt->port_num	= port_num;
531 	pkt->hdr	= skb_put_zero(skb, paylen);
532 	pkt->mask	|= RXE_GRH_MASK;
533 
534 out:
535 	rdma_put_gid_attr(attr);
536 	return skb;
537 }
538 
539 /*
540  * this is required by rxe_cfg to match rxe devices in
541  * /sys/class/infiniband up with their underlying ethernet devices
542  */
543 const char *rxe_parent_name(struct rxe_dev *rxe, unsigned int port_num)
544 {
545 	return rxe->ndev->name;
546 }
547 
548 enum rdma_link_layer rxe_link_layer(struct rxe_dev *rxe, unsigned int port_num)
549 {
550 	return IB_LINK_LAYER_ETHERNET;
551 }
552 
553 struct rxe_dev *rxe_net_add(struct net_device *ndev)
554 {
555 	int err;
556 	struct rxe_dev *rxe = NULL;
557 
558 	rxe = (struct rxe_dev *)ib_alloc_device(sizeof(*rxe));
559 	if (!rxe)
560 		return NULL;
561 
562 	rxe->ndev = ndev;
563 
564 	err = rxe_add(rxe, ndev->mtu);
565 	if (err) {
566 		ib_dealloc_device(&rxe->ib_dev);
567 		return NULL;
568 	}
569 
570 	spin_lock_bh(&dev_list_lock);
571 	list_add_tail(&rxe->list, &rxe_dev_list);
572 	spin_unlock_bh(&dev_list_lock);
573 	return rxe;
574 }
575 
576 void rxe_remove_all(void)
577 {
578 	spin_lock_bh(&dev_list_lock);
579 	while (!list_empty(&rxe_dev_list)) {
580 		struct rxe_dev *rxe =
581 			list_first_entry(&rxe_dev_list, struct rxe_dev, list);
582 
583 		list_del(&rxe->list);
584 		spin_unlock_bh(&dev_list_lock);
585 		rxe_remove(rxe);
586 		spin_lock_bh(&dev_list_lock);
587 	}
588 	spin_unlock_bh(&dev_list_lock);
589 }
590 
591 static void rxe_port_event(struct rxe_dev *rxe,
592 			   enum ib_event_type event)
593 {
594 	struct ib_event ev;
595 
596 	ev.device = &rxe->ib_dev;
597 	ev.element.port_num = 1;
598 	ev.event = event;
599 
600 	ib_dispatch_event(&ev);
601 }
602 
603 /* Caller must hold net_info_lock */
604 void rxe_port_up(struct rxe_dev *rxe)
605 {
606 	struct rxe_port *port;
607 
608 	port = &rxe->port;
609 	port->attr.state = IB_PORT_ACTIVE;
610 
611 	rxe_port_event(rxe, IB_EVENT_PORT_ACTIVE);
612 	dev_info(&rxe->ib_dev.dev, "set active\n");
613 }
614 
615 /* Caller must hold net_info_lock */
616 void rxe_port_down(struct rxe_dev *rxe)
617 {
618 	struct rxe_port *port;
619 
620 	port = &rxe->port;
621 	port->attr.state = IB_PORT_DOWN;
622 
623 	rxe_port_event(rxe, IB_EVENT_PORT_ERR);
624 	rxe_counter_inc(rxe, RXE_CNT_LINK_DOWNED);
625 	dev_info(&rxe->ib_dev.dev, "set down\n");
626 }
627 
628 void rxe_set_port_state(struct rxe_dev *rxe)
629 {
630 	if (netif_running(rxe->ndev) && netif_carrier_ok(rxe->ndev))
631 		rxe_port_up(rxe);
632 	else
633 		rxe_port_down(rxe);
634 }
635 
636 static int rxe_notify(struct notifier_block *not_blk,
637 		      unsigned long event,
638 		      void *arg)
639 {
640 	struct net_device *ndev = netdev_notifier_info_to_dev(arg);
641 	struct rxe_dev *rxe = net_to_rxe(ndev);
642 
643 	if (!rxe)
644 		goto out;
645 
646 	switch (event) {
647 	case NETDEV_UNREGISTER:
648 		list_del(&rxe->list);
649 		rxe_remove(rxe);
650 		break;
651 	case NETDEV_UP:
652 		rxe_port_up(rxe);
653 		break;
654 	case NETDEV_DOWN:
655 		rxe_port_down(rxe);
656 		break;
657 	case NETDEV_CHANGEMTU:
658 		pr_info("%s changed mtu to %d\n", ndev->name, ndev->mtu);
659 		rxe_set_mtu(rxe, ndev->mtu);
660 		break;
661 	case NETDEV_CHANGE:
662 		rxe_set_port_state(rxe);
663 		break;
664 	case NETDEV_REBOOT:
665 	case NETDEV_GOING_DOWN:
666 	case NETDEV_CHANGEADDR:
667 	case NETDEV_CHANGENAME:
668 	case NETDEV_FEAT_CHANGE:
669 	default:
670 		pr_info("ignoring netdev event = %ld for %s\n",
671 			event, ndev->name);
672 		break;
673 	}
674 out:
675 	return NOTIFY_OK;
676 }
677 
678 static struct notifier_block rxe_net_notifier = {
679 	.notifier_call = rxe_notify,
680 };
681 
682 static int rxe_net_ipv4_init(void)
683 {
684 	recv_sockets.sk4 = rxe_setup_udp_tunnel(&init_net,
685 				htons(ROCE_V2_UDP_DPORT), false);
686 	if (IS_ERR(recv_sockets.sk4)) {
687 		recv_sockets.sk4 = NULL;
688 		pr_err("Failed to create IPv4 UDP tunnel\n");
689 		return -1;
690 	}
691 
692 	return 0;
693 }
694 
695 static int rxe_net_ipv6_init(void)
696 {
697 #if IS_ENABLED(CONFIG_IPV6)
698 
699 	recv_sockets.sk6 = rxe_setup_udp_tunnel(&init_net,
700 						htons(ROCE_V2_UDP_DPORT), true);
701 	if (IS_ERR(recv_sockets.sk6)) {
702 		recv_sockets.sk6 = NULL;
703 		pr_err("Failed to create IPv6 UDP tunnel\n");
704 		return -1;
705 	}
706 #endif
707 	return 0;
708 }
709 
710 void rxe_net_exit(void)
711 {
712 	rxe_release_udp_tunnel(recv_sockets.sk6);
713 	rxe_release_udp_tunnel(recv_sockets.sk4);
714 	unregister_netdevice_notifier(&rxe_net_notifier);
715 }
716 
717 int rxe_net_init(void)
718 {
719 	int err;
720 
721 	recv_sockets.sk6 = NULL;
722 
723 	err = rxe_net_ipv4_init();
724 	if (err)
725 		return err;
726 	err = rxe_net_ipv6_init();
727 	if (err)
728 		goto err_out;
729 	err = register_netdevice_notifier(&rxe_net_notifier);
730 	if (err) {
731 		pr_err("Failed to register netdev notifier\n");
732 		goto err_out;
733 	}
734 	return 0;
735 err_out:
736 	rxe_net_exit();
737 	return err;
738 }
739