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