1 /*        $NetBSD: udp_usrreq.c,v 1.266 2024/10/08 02:30:04 riastradh Exp $     */
2 
3 /*
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
34  *        The Regents of the University of California.  All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. Neither the name of the University nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  *
60  *        @(#)udp_usrreq.c    8.6 (Berkeley) 5/23/95
61  */
62 
63 /*
64  * UDP protocol implementation.
65  * Per RFC 768, August, 1980.
66  */
67 
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c,v 1.266 2024/10/08 02:30:04 riastradh Exp $");
70 
71 #ifdef _KERNEL_OPT
72 #include "opt_inet.h"
73 #include "opt_ipsec.h"
74 #include "opt_inet_csum.h"
75 #include "opt_mbuftrace.h"
76 #include "opt_net_mpsafe.h"
77 #endif
78 
79 #include <sys/param.h>
80 #include <sys/mbuf.h>
81 #include <sys/once.h>
82 #include <sys/protosw.h>
83 #include <sys/socket.h>
84 #include <sys/socketvar.h>
85 #include <sys/systm.h>
86 #include <sys/proc.h>
87 #include <sys/domain.h>
88 #include <sys/sysctl.h>
89 
90 #include <net/if.h>
91 
92 #include <netinet/in.h>
93 #include <netinet/in_systm.h>
94 #include <netinet/in_var.h>
95 #include <netinet/ip.h>
96 #include <netinet/in_pcb.h>
97 #include <netinet/ip_var.h>
98 #include <netinet/ip_icmp.h>
99 #include <netinet/udp.h>
100 #include <netinet/udp_var.h>
101 #include <netinet/udp_private.h>
102 
103 #ifdef INET6
104 #include <netinet/ip6.h>
105 #include <netinet6/ip6_var.h>
106 #include <netinet6/ip6_private.h>
107 #include <netinet6/in6_pcb.h>
108 #include <netinet6/udp6_var.h>
109 #include <netinet6/udp6_private.h>
110 #endif
111 
112 #ifndef INET6
113 #include <netinet/ip6.h>
114 #endif
115 
116 #ifdef IPSEC
117 #include <netipsec/ipsec.h>
118 #include <netipsec/esp.h>
119 #endif
120 
121 int udpcksum = 1;
122 int udp_do_loopback_cksum = 0;
123 
124 struct inpcbtable udbtable;
125 
126 percpu_t *udpstat_percpu;
127 
128 #ifdef INET
129 #ifdef IPSEC
130 static int udp4_espinudp(struct mbuf **, int);
131 #endif
132 static void udp4_sendup(struct mbuf *, int, struct sockaddr *,
133     struct socket *);
134 static int udp4_realinput(struct sockaddr_in *, struct sockaddr_in *,
135     struct mbuf **, int);
136 static int udp4_input_checksum(struct mbuf *, const struct udphdr *, int, int);
137 #endif
138 #ifdef INET
139 static void udp_notify (struct inpcb *, int);
140 #endif
141 
142 #ifndef UDBHASHSIZE
143 #define   UDBHASHSIZE         128
144 #endif
145 int udbhashsize = UDBHASHSIZE;
146 
147 /*
148  * For send - really max datagram size; for receive - 40 1K datagrams.
149  */
150 static int udp_sendspace = 9216;
151 static int udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
152 
153 #ifdef MBUFTRACE
154 struct mowner udp_mowner = MOWNER_INIT("udp", "");
155 struct mowner udp_rx_mowner = MOWNER_INIT("udp", "rx");
156 struct mowner udp_tx_mowner = MOWNER_INIT("udp", "tx");
157 #endif
158 
159 #ifdef UDP_CSUM_COUNTERS
160 #include <sys/device.h>
161 
162 #if defined(INET)
163 struct evcnt udp_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
164     NULL, "udp", "hwcsum bad");
165 struct evcnt udp_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
166     NULL, "udp", "hwcsum ok");
167 struct evcnt udp_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
168     NULL, "udp", "hwcsum data");
169 struct evcnt udp_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
170     NULL, "udp", "swcsum");
171 
172 EVCNT_ATTACH_STATIC(udp_hwcsum_bad);
173 EVCNT_ATTACH_STATIC(udp_hwcsum_ok);
174 EVCNT_ATTACH_STATIC(udp_hwcsum_data);
175 EVCNT_ATTACH_STATIC(udp_swcsum);
176 #endif /* defined(INET) */
177 
178 #define   UDP_CSUM_COUNTER_INCR(ev)     (ev)->ev_count++
179 #else
180 #define   UDP_CSUM_COUNTER_INCR(ev)     /* nothing */
181 #endif /* UDP_CSUM_COUNTERS */
182 
183 static void sysctl_net_inet_udp_setup(struct sysctllog **);
184 
185 static int
do_udpinit(void)186 do_udpinit(void)
187 {
188 
189           inpcb_init(&udbtable, udbhashsize, udbhashsize);
190           udpstat_percpu = percpu_alloc(sizeof(uint64_t) * UDP_NSTATS);
191 
192           MOWNER_ATTACH(&udp_tx_mowner);
193           MOWNER_ATTACH(&udp_rx_mowner);
194           MOWNER_ATTACH(&udp_mowner);
195 
196           return 0;
197 }
198 
199 void
udp_init_common(void)200 udp_init_common(void)
201 {
202           static ONCE_DECL(doudpinit);
203 
204           RUN_ONCE(&doudpinit, do_udpinit);
205 }
206 
207 void
udp_init(void)208 udp_init(void)
209 {
210 
211           sysctl_net_inet_udp_setup(NULL);
212 
213           udp_init_common();
214 }
215 
216 /*
217  * Checksum extended UDP header and data.
218  */
219 int
udp_input_checksum(int af,struct mbuf * m,const struct udphdr * uh,int iphlen,int len)220 udp_input_checksum(int af, struct mbuf *m, const struct udphdr *uh,
221     int iphlen, int len)
222 {
223 
224           switch (af) {
225 #ifdef INET
226           case AF_INET:
227                     return udp4_input_checksum(m, uh, iphlen, len);
228 #endif
229 #ifdef INET6
230           case AF_INET6:
231                     return udp6_input_checksum(m, uh, iphlen, len);
232 #endif
233           }
234 #ifdef DIAGNOSTIC
235           panic("udp_input_checksum: unknown af %d", af);
236 #endif
237           /* NOTREACHED */
238           return -1;
239 }
240 
241 #ifdef INET
242 
243 /*
244  * Checksum extended UDP header and data.
245  */
246 static int
udp4_input_checksum(struct mbuf * m,const struct udphdr * uh,int iphlen,int len)247 udp4_input_checksum(struct mbuf *m, const struct udphdr *uh,
248     int iphlen, int len)
249 {
250 
251           /*
252            * XXX it's better to record and check if this mbuf is
253            * already checked.
254            */
255 
256           if (uh->uh_sum == 0)
257                     return 0;
258 
259           switch (m->m_pkthdr.csum_flags &
260               ((m_get_rcvif_NOMPSAFE(m)->if_csum_flags_rx & M_CSUM_UDPv4) |
261               M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
262           case M_CSUM_UDPv4|M_CSUM_TCP_UDP_BAD:
263                     UDP_CSUM_COUNTER_INCR(&udp_hwcsum_bad);
264                     goto badcsum;
265 
266           case M_CSUM_UDPv4|M_CSUM_DATA: {
267                     u_int32_t hw_csum = m->m_pkthdr.csum_data;
268 
269                     UDP_CSUM_COUNTER_INCR(&udp_hwcsum_data);
270                     if (m->m_pkthdr.csum_flags & M_CSUM_NO_PSEUDOHDR) {
271                               const struct ip *ip =
272                                   mtod(m, const struct ip *);
273 
274                               hw_csum = in_cksum_phdr(ip->ip_src.s_addr,
275                                   ip->ip_dst.s_addr,
276                                   htons(hw_csum + len + IPPROTO_UDP));
277                     }
278                     if ((hw_csum ^ 0xffff) != 0)
279                               goto badcsum;
280                     break;
281           }
282 
283           case M_CSUM_UDPv4:
284                     /* Checksum was okay. */
285                     UDP_CSUM_COUNTER_INCR(&udp_hwcsum_ok);
286                     break;
287 
288           default:
289                     /*
290                      * Need to compute it ourselves.  Maybe skip checksum
291                      * on loopback interfaces.
292                      */
293                     if (__predict_true(!(m_get_rcvif_NOMPSAFE(m)->if_flags &
294                                              IFF_LOOPBACK) ||
295                                            udp_do_loopback_cksum)) {
296                               UDP_CSUM_COUNTER_INCR(&udp_swcsum);
297                               if (in4_cksum(m, IPPROTO_UDP, iphlen, len) != 0)
298                                         goto badcsum;
299                     }
300                     break;
301           }
302 
303           return 0;
304 
305 badcsum:
306           UDP_STATINC(UDP_STAT_BADSUM);
307           return -1;
308 }
309 
310 void
udp_input(struct mbuf * m,int off,int proto)311 udp_input(struct mbuf *m, int off, int proto)
312 {
313           struct sockaddr_in src, dst;
314           struct ip *ip;
315           struct udphdr *uh;
316           int iphlen = off;
317           int len;
318           int n;
319           u_int16_t ip_len;
320 
321           MCLAIM(m, &udp_rx_mowner);
322           UDP_STATINC(UDP_STAT_IPACKETS);
323 
324           /*
325            * Get IP and UDP header together in first mbuf.
326            */
327           ip = mtod(m, struct ip *);
328           M_REGION_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr));
329           if (uh == NULL) {
330                     UDP_STATINC(UDP_STAT_HDROPS);
331                     return;
332           }
333 
334           /*
335            * Enforce alignment requirements that are violated in
336            * some cases, see kern/50766 for details.
337            */
338           if (ACCESSIBLE_POINTER(uh, struct udphdr) == 0) {
339                     m = m_copyup(m, iphlen + sizeof(struct udphdr), 0);
340                     if (m == NULL) {
341                               UDP_STATINC(UDP_STAT_HDROPS);
342                               return;
343                     }
344                     ip = mtod(m, struct ip *);
345                     uh = (struct udphdr *)(mtod(m, char *) + iphlen);
346           }
347           KASSERT(ACCESSIBLE_POINTER(uh, struct udphdr));
348 
349           /* destination port of 0 is illegal, based on RFC768. */
350           if (uh->uh_dport == 0)
351                     goto bad;
352 
353           /*
354            * Make mbuf data length reflect UDP length.
355            * If not enough data to reflect UDP length, drop.
356            */
357           ip_len = ntohs(ip->ip_len);
358           len = ntohs((u_int16_t)uh->uh_ulen);
359           if (len < sizeof(struct udphdr)) {
360                     UDP_STATINC(UDP_STAT_BADLEN);
361                     goto bad;
362           }
363           if (ip_len != iphlen + len) {
364                     if (ip_len < iphlen + len) {
365                               UDP_STATINC(UDP_STAT_BADLEN);
366                               goto bad;
367                     }
368                     m_adj(m, iphlen + len - ip_len);
369           }
370 
371           /*
372            * Checksum extended UDP header and data.
373            */
374           if (udp4_input_checksum(m, uh, iphlen, len))
375                     goto badcsum;
376 
377           /* construct source and dst sockaddrs. */
378           sockaddr_in_init(&src, &ip->ip_src, uh->uh_sport);
379           sockaddr_in_init(&dst, &ip->ip_dst, uh->uh_dport);
380 
381           if ((n = udp4_realinput(&src, &dst, &m, iphlen)) == -1) {
382                     UDP_STATINC(UDP_STAT_HDROPS);
383                     return;
384           }
385           if (m == NULL) {
386                     /*
387                      * packet has been processed by ESP stuff -
388                      * e.g. dropped NAT-T-keep-alive-packet ...
389                      */
390                     return;
391           }
392 
393           ip = mtod(m, struct ip *);
394           M_REGION_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr));
395           if (uh == NULL) {
396                     UDP_STATINC(UDP_STAT_HDROPS);
397                     return;
398           }
399           /* XXX Re-enforce alignment? */
400 
401 #ifdef INET6
402           if (IN_MULTICAST(ip->ip_dst.s_addr) || n == 0) {
403                     struct sockaddr_in6 src6, dst6;
404 
405                     memset(&src6, 0, sizeof(src6));
406                     src6.sin6_family = AF_INET6;
407                     src6.sin6_len = sizeof(struct sockaddr_in6);
408                     in6_in_2_v4mapin6(&ip->ip_src, &src6.sin6_addr);
409                     src6.sin6_port = uh->uh_sport;
410                     memset(&dst6, 0, sizeof(dst6));
411                     dst6.sin6_family = AF_INET6;
412                     dst6.sin6_len = sizeof(struct sockaddr_in6);
413                     in6_in_2_v4mapin6(&ip->ip_dst, &dst6.sin6_addr);
414                     dst6.sin6_port = uh->uh_dport;
415 
416                     n += udp6_realinput(AF_INET, &src6, &dst6, &m, iphlen);
417           }
418 #endif
419 
420           if (n == 0) {
421                     if (m->m_flags & (M_BCAST | M_MCAST)) {
422                               UDP_STATINC(UDP_STAT_NOPORTBCAST);
423                               goto bad;
424                     }
425                     UDP_STATINC(UDP_STAT_NOPORT);
426                     icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
427                     m = NULL;
428           }
429 
430 bad:
431           m_freem(m);
432           return;
433 
434 badcsum:
435           m_freem(m);
436 }
437 #endif
438 
439 #ifdef INET
440 static void
udp4_sendup(struct mbuf * m,int off,struct sockaddr * src,struct socket * so)441 udp4_sendup(struct mbuf *m, int off /* offset of data portion */,
442     struct sockaddr *src, struct socket *so)
443 {
444           struct mbuf *opts = NULL;
445           struct mbuf *n;
446           struct inpcb *inp;
447 
448           KASSERT(so != NULL);
449           KASSERT(so->so_proto->pr_domain->dom_family == AF_INET);
450           inp = sotoinpcb(so);
451           KASSERT(inp != NULL);
452 
453 #if defined(IPSEC)
454           if (ipsec_used && ipsec_in_reject(m, inp)) {
455                     if ((n = m_copypacket(m, M_DONTWAIT)) != NULL)
456                               icmp_error(n, ICMP_UNREACH, ICMP_UNREACH_ADMIN_PROHIBIT,
457                                   0, 0);
458                     return;
459           }
460 #endif
461 
462           if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
463                     if (inp->inp_flags & INP_CONTROLOPTS ||
464                         SOOPT_TIMESTAMP(so->so_options)) {
465                               struct ip *ip = mtod(n, struct ip *);
466                               ip_savecontrol(inp, &opts, ip, n);
467                     }
468 
469                     m_adj(n, off);
470                     if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) {
471                               m_freem(n);
472                               m_freem(opts);
473                               UDP_STATINC(UDP_STAT_FULLSOCK);
474                               soroverflow(so);
475                     } else
476                               sorwakeup(so);
477           }
478 }
479 #endif
480 
481 #ifdef INET
482 static int
udp4_realinput(struct sockaddr_in * src,struct sockaddr_in * dst,struct mbuf ** mp,int off)483 udp4_realinput(struct sockaddr_in *src, struct sockaddr_in *dst,
484     struct mbuf **mp, int off /* offset of udphdr */)
485 {
486           u_int16_t *sport, *dport;
487           int rcvcnt;
488           struct in_addr *src4, *dst4;
489           struct inpcb *inp;
490           struct mbuf *m = *mp;
491 
492           rcvcnt = 0;
493           off += sizeof(struct udphdr); /* now, offset of payload */
494 
495           if (src->sin_family != AF_INET || dst->sin_family != AF_INET)
496                     goto bad;
497 
498           src4 = &src->sin_addr;
499           sport = &src->sin_port;
500           dst4 = &dst->sin_addr;
501           dport = &dst->sin_port;
502 
503           if (IN_MULTICAST(dst4->s_addr) ||
504               in_broadcast(*dst4, m_get_rcvif_NOMPSAFE(m))) {
505                     /*
506                      * Deliver a multicast or broadcast datagram to *all* sockets
507                      * for which the local and remote addresses and ports match
508                      * those of the incoming datagram.  This allows more than
509                      * one process to receive multi/broadcasts on the same port.
510                      * (This really ought to be done for unicast datagrams as
511                      * well, but that would cause problems with existing
512                      * applications that open both address-specific sockets and
513                      * a wildcard socket listening to the same port -- they would
514                      * end up receiving duplicates of every unicast datagram.
515                      * Those applications open the multiple sockets to overcome an
516                      * inadequacy of the UDP socket interface, but for backwards
517                      * compatibility we avoid the problem here rather than
518                      * fixing the interface.  Maybe 4.5BSD will remedy this?)
519                      */
520 
521                     /*
522                      * KAME note: traditionally we dropped udpiphdr from mbuf here.
523                      * we need udpiphdr for IPsec processing so we do that later.
524                      */
525                     /*
526                      * Locate pcb(s) for datagram.
527                      */
528                     TAILQ_FOREACH(inp, &udbtable.inpt_queue, inp_queue) {
529                               if (inp->inp_af != AF_INET)
530                                         continue;
531 
532                               if (inp->inp_lport != *dport)
533                                         continue;
534                               if (!in_nullhost(in4p_laddr(inp))) {
535                                         if (!in_hosteq(in4p_laddr(inp), *dst4))
536                                                   continue;
537                               }
538                               if (!in_nullhost(in4p_faddr(inp))) {
539                                         if (!in_hosteq(in4p_faddr(inp), *src4) ||
540                                             inp->inp_fport != *sport)
541                                                   continue;
542                               }
543 
544                               udp4_sendup(m, off, (struct sockaddr *)src,
545                                   inp->inp_socket);
546                               rcvcnt++;
547 
548                               /*
549                                * Don't look for additional matches if this one does
550                                * not have either the SO_REUSEPORT or SO_REUSEADDR
551                                * socket options set.  This heuristic avoids searching
552                                * through all pcbs in the common case of a non-shared
553                                * port.  It assumes that an application will never
554                                * clear these options after setting them.
555                                */
556                               if ((inp->inp_socket->so_options &
557                                   (SO_REUSEPORT|SO_REUSEADDR)) == 0)
558                                         break;
559                     }
560           } else {
561                     /*
562                      * Locate pcb for datagram.
563                      */
564                     inp = inpcb_lookup(&udbtable, *src4, *sport, *dst4,
565                         *dport, 0);
566                     if (inp == 0) {
567                               UDP_STATINC(UDP_STAT_PCBHASHMISS);
568                               inp = inpcb_lookup_bound(&udbtable, *dst4, *dport);
569                               if (inp == 0)
570                                         return rcvcnt;
571                     }
572 
573 #ifdef IPSEC
574                     /* Handle ESP over UDP */
575                     if (inp->inp_flags & INP_ESPINUDP) {
576                               switch (udp4_espinudp(mp, off)) {
577                               case -1: /* Error, m was freed */
578                                         KASSERT(*mp == NULL);
579                                         rcvcnt = -1;
580                                         goto bad;
581 
582                               case 1: /* ESP over UDP */
583                                         KASSERT(*mp == NULL);
584                                         rcvcnt++;
585                                         goto bad;
586 
587                               case 0: /* plain UDP */
588                               default: /* Unexpected */
589                                         /*
590                                          * Normal UDP processing will take place,
591                                          * m may have changed.
592                                          */
593                                         m = *mp;
594                                         break;
595                               }
596                     }
597 #endif
598                     if (inp->inp_overudp_cb != NULL) {
599                               int ret;
600                               ret = inp->inp_overudp_cb(mp, off, inp->inp_socket,
601                                   sintosa(src), inp->inp_overudp_arg);
602                               switch (ret) {
603                               case -1: /* Error, m was freed */
604                                         KASSERT(*mp == NULL);
605                                         rcvcnt = -1;
606                                         goto bad;
607 
608                               case 1: /* Foo over UDP */
609                                         KASSERT(*mp == NULL);
610                                         rcvcnt++;
611                                         goto bad;
612 
613                               case 0: /* plain UDP */
614                               default: /* Unexpected */
615                                         /*
616                                          * Normal UDP processing will take place,
617                                          * m may have changed.
618                                          */
619                                         m = *mp;
620                                         break;
621                               }
622                     }
623 
624                     /*
625                      * Check the minimum TTL for socket.
626                      */
627                     if (mtod(m, struct ip *)->ip_ttl < in4p_ip_minttl(inp))
628                               goto bad;
629 
630                     udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket);
631                     rcvcnt++;
632           }
633 
634 bad:
635           return rcvcnt;
636 }
637 #endif
638 
639 #ifdef INET
640 /*
641  * Notify a udp user of an asynchronous error;
642  * just wake up so that he can collect error status.
643  */
644 static void
udp_notify(struct inpcb * inp,int errno)645 udp_notify(struct inpcb *inp, int errno)
646 {
647           inp->inp_socket->so_error = errno;
648           sorwakeup(inp->inp_socket);
649           sowwakeup(inp->inp_socket);
650 }
651 
652 void *
udp_ctlinput(int cmd,const struct sockaddr * sa,void * v)653 udp_ctlinput(int cmd, const struct sockaddr *sa, void *v)
654 {
655           struct ip *ip = v;
656           struct udphdr *uh;
657           void (*notify)(struct inpcb *, int) = udp_notify;
658           int errno;
659 
660           if (sa->sa_family != AF_INET ||
661               sa->sa_len != sizeof(struct sockaddr_in))
662                     return NULL;
663           if ((unsigned)cmd >= PRC_NCMDS)
664                     return NULL;
665 
666           errno = inetctlerrmap[cmd];
667           if (PRC_IS_REDIRECT(cmd)) {
668                     notify = inpcb_rtchange;
669                     ip = NULL;
670           } else if (cmd == PRC_HOSTDEAD) {
671                     ip = NULL;
672           } else if (errno == 0) {
673                     return NULL;
674           }
675 
676           if (ip) {
677                     uh = (struct udphdr *)((char *)ip + (ip->ip_hl << 2));
678                     inpcb_notify(&udbtable, satocsin(sa)->sin_addr, uh->uh_dport,
679                         ip->ip_src, uh->uh_sport, errno, notify);
680                     /* XXX mapped address case */
681           } else {
682                     inpcb_notifyall(&udbtable, satocsin(sa)->sin_addr, errno,
683                         notify);
684           }
685 
686           return NULL;
687 }
688 
689 int
udp_ctloutput(int op,struct socket * so,struct sockopt * sopt)690 udp_ctloutput(int op, struct socket *so, struct sockopt *sopt)
691 {
692           int s;
693           int error = 0;
694           struct inpcb *inp;
695           int family;
696           int optval;
697 
698           family = so->so_proto->pr_domain->dom_family;
699 
700           s = splsoftnet();
701           switch (family) {
702 #ifdef INET
703           case PF_INET:
704                     if (sopt->sopt_level != IPPROTO_UDP) {
705                               error = ip_ctloutput(op, so, sopt);
706                               goto end;
707                     }
708                     break;
709 #endif
710 #ifdef INET6
711           case PF_INET6:
712                     if (sopt->sopt_level != IPPROTO_UDP) {
713                               error = ip6_ctloutput(op, so, sopt);
714                               goto end;
715                     }
716                     break;
717 #endif
718           default:
719                     error = EAFNOSUPPORT;
720                     goto end;
721           }
722 
723 
724           switch (op) {
725           case PRCO_SETOPT:
726                     inp = sotoinpcb(so);
727 
728                     switch (sopt->sopt_name) {
729                     case UDP_ENCAP:
730                               error = sockopt_getint(sopt, &optval);
731                               if (error)
732                                         break;
733 
734                               switch(optval) {
735                               case 0:
736                                         inp->inp_flags &= ~INP_ESPINUDP;
737                                         break;
738 
739                               case UDP_ENCAP_ESPINUDP:
740                                         inp->inp_flags |= INP_ESPINUDP;
741                                         break;
742 
743                               default:
744                                         error = EINVAL;
745                                         break;
746                               }
747                               break;
748 
749                     default:
750                               error = ENOPROTOOPT;
751                               break;
752                     }
753                     break;
754 
755           default:
756                     error = EINVAL;
757                     break;
758           }
759 
760 end:
761           splx(s);
762           return error;
763 }
764 
765 int
udp_output(struct mbuf * m,struct inpcb * inp,struct mbuf * control,struct lwp * l)766 udp_output(struct mbuf *m, struct inpcb *inp, struct mbuf *control,
767     struct lwp *l)
768 {
769           struct udpiphdr *ui;
770           struct route *ro;
771           struct ip_pktopts pktopts;
772           kauth_cred_t cred;
773           int len = m->m_pkthdr.len;
774           int error, flags = 0;
775 
776           MCLAIM(m, &udp_tx_mowner);
777 
778           /*
779            * Calculate data length and get a mbuf
780            * for UDP and IP headers.
781            */
782           M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
783           if (m == NULL) {
784                     error = ENOBUFS;
785                     goto release;
786           }
787 
788           /*
789            * Compute the packet length of the IP header, and
790            * punt if the length looks bogus.
791            */
792           if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
793                     error = EMSGSIZE;
794                     goto release;
795           }
796 
797           if (l == NULL)
798                     cred = NULL;
799           else
800                     cred = l->l_cred;
801 
802           /* Setup IP outgoing packet options */
803           memset(&pktopts, 0, sizeof(pktopts));
804           error = ip_setpktopts(control, &pktopts, &flags, inp, cred);
805           if (error != 0)
806                     goto release;
807 
808           m_freem(control);
809           control = NULL;
810 
811           /*
812            * Fill in mbuf with extended UDP header
813            * and addresses and length put into network format.
814            */
815           ui = mtod(m, struct udpiphdr *);
816           ui->ui_pr = IPPROTO_UDP;
817           ui->ui_src = pktopts.ippo_laddr.sin_addr;
818           ui->ui_dst = in4p_faddr(inp);
819           ui->ui_sport = inp->inp_lport;
820           ui->ui_dport = inp->inp_fport;
821           ui->ui_ulen = htons((u_int16_t)len + sizeof(struct udphdr));
822 
823           ro = &inp->inp_route;
824 
825           /*
826            * Set up checksum and output datagram.
827            */
828           if (udpcksum) {
829                     /*
830                      * XXX Cache pseudo-header checksum part for
831                      * XXX "connected" UDP sockets.
832                      */
833                     ui->ui_sum = in_cksum_phdr(ui->ui_src.s_addr,
834                         ui->ui_dst.s_addr, htons((u_int16_t)len +
835                         sizeof(struct udphdr) + IPPROTO_UDP));
836                     m->m_pkthdr.csum_flags = M_CSUM_UDPv4;
837                     m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
838           } else
839                     ui->ui_sum = 0;
840 
841           ((struct ip *)ui)->ip_len = htons(sizeof(struct udpiphdr) + len);
842           ((struct ip *)ui)->ip_ttl = in4p_ip(inp).ip_ttl;  /* XXX */
843           ((struct ip *)ui)->ip_tos = in4p_ip(inp).ip_tos;  /* XXX */
844           UDP_STATINC(UDP_STAT_OPACKETS);
845 
846           flags |= inp->inp_socket->so_options & (SO_DONTROUTE|SO_BROADCAST);
847           return ip_output(m, inp->inp_options, ro, flags, pktopts.ippo_imo, inp);
848 
849  release:
850           m_freem(control);
851           m_freem(m);
852           return error;
853 }
854 
855 static int
udp_attach(struct socket * so,int proto)856 udp_attach(struct socket *so, int proto)
857 {
858           struct inpcb *inp;
859           int error;
860 
861           KASSERT(sotoinpcb(so) == NULL);
862 
863           /* Assign the lock (must happen even if we will error out). */
864           sosetlock(so);
865 
866 #ifdef MBUFTRACE
867           so->so_mowner = &udp_mowner;
868           so->so_rcv.sb_mowner = &udp_rx_mowner;
869           so->so_snd.sb_mowner = &udp_tx_mowner;
870 #endif
871           if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
872                     error = soreserve(so, udp_sendspace, udp_recvspace);
873                     if (error) {
874                               return error;
875                     }
876           }
877 
878           error = inpcb_create(so, &udbtable);
879           if (error) {
880                     return error;
881           }
882           inp = sotoinpcb(so);
883           in4p_ip(inp).ip_ttl = ip_defttl;
884           KASSERT(solocked(so));
885 
886           return error;
887 }
888 
889 static void
udp_detach(struct socket * so)890 udp_detach(struct socket *so)
891 {
892           struct inpcb *inp;
893 
894           KASSERT(solocked(so));
895           inp = sotoinpcb(so);
896           KASSERT(inp != NULL);
897           inpcb_destroy(inp);
898 }
899 
900 static int
udp_accept(struct socket * so,struct sockaddr * nam)901 udp_accept(struct socket *so, struct sockaddr *nam)
902 {
903           KASSERT(solocked(so));
904 
905           panic("udp_accept");
906 
907           return EOPNOTSUPP;
908 }
909 
910 static int
udp_bind(struct socket * so,struct sockaddr * nam,struct lwp * l)911 udp_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
912 {
913           struct inpcb *inp = sotoinpcb(so);
914           struct sockaddr_in *sin = (struct sockaddr_in *)nam;
915           int error = 0;
916           int s;
917 
918           KASSERT(solocked(so));
919           KASSERT(inp != NULL);
920           KASSERT(nam != NULL);
921 
922           s = splsoftnet();
923           error = inpcb_bind(inp, sin, l);
924           splx(s);
925 
926           return error;
927 }
928 
929 static int
udp_listen(struct socket * so,struct lwp * l)930 udp_listen(struct socket *so, struct lwp *l)
931 {
932           KASSERT(solocked(so));
933 
934           return EOPNOTSUPP;
935 }
936 
937 static int
udp_connect(struct socket * so,struct sockaddr * nam,struct lwp * l)938 udp_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
939 {
940           struct inpcb *inp = sotoinpcb(so);
941           int error = 0;
942           int s;
943 
944           KASSERT(solocked(so));
945           KASSERT(inp != NULL);
946           KASSERT(nam != NULL);
947 
948           s = splsoftnet();
949           error = inpcb_connect(inp, (struct sockaddr_in *)nam, l);
950           if (! error)
951                     soisconnected(so);
952           splx(s);
953           return error;
954 }
955 
956 static int
udp_connect2(struct socket * so,struct socket * so2)957 udp_connect2(struct socket *so, struct socket *so2)
958 {
959           KASSERT(solocked(so));
960 
961           return EOPNOTSUPP;
962 }
963 
964 static int
udp_disconnect(struct socket * so)965 udp_disconnect(struct socket *so)
966 {
967           struct inpcb *inp = sotoinpcb(so);
968           int s;
969 
970           KASSERT(solocked(so));
971           KASSERT(inp != NULL);
972 
973           s = splsoftnet();
974           /*soisdisconnected(so);*/
975           so->so_state &= ~SS_ISCONNECTED;        /* XXX */
976           inpcb_disconnect(inp);
977           in4p_laddr(inp) = zeroin_addr;                    /* XXX */
978           inpcb_set_state(inp, INP_BOUND);                  /* XXX */
979           splx(s);
980 
981           return 0;
982 }
983 
984 static int
udp_shutdown(struct socket * so)985 udp_shutdown(struct socket *so)
986 {
987           int s;
988 
989           KASSERT(solocked(so));
990 
991           s = splsoftnet();
992           socantsendmore(so);
993           splx(s);
994 
995           return 0;
996 }
997 
998 static int
udp_abort(struct socket * so)999 udp_abort(struct socket *so)
1000 {
1001           KASSERT(solocked(so));
1002 
1003           panic("udp_abort");
1004 
1005           return EOPNOTSUPP;
1006 }
1007 
1008 static int
udp_ioctl(struct socket * so,u_long cmd,void * nam,struct ifnet * ifp)1009 udp_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
1010 {
1011           return in_control(so, cmd, nam, ifp);
1012 }
1013 
1014 static int
udp_stat(struct socket * so,struct stat * ub)1015 udp_stat(struct socket *so, struct stat *ub)
1016 {
1017           KASSERT(solocked(so));
1018 
1019           /* stat: don't bother with a blocksize. */
1020           return 0;
1021 }
1022 
1023 static int
udp_peeraddr(struct socket * so,struct sockaddr * nam)1024 udp_peeraddr(struct socket *so, struct sockaddr *nam)
1025 {
1026           int s;
1027 
1028           KASSERT(solocked(so));
1029           KASSERT(sotoinpcb(so) != NULL);
1030           KASSERT(nam != NULL);
1031 
1032           s = splsoftnet();
1033           inpcb_fetch_peeraddr(sotoinpcb(so), (struct sockaddr_in *)nam);
1034           splx(s);
1035 
1036           return 0;
1037 }
1038 
1039 static int
udp_sockaddr(struct socket * so,struct sockaddr * nam)1040 udp_sockaddr(struct socket *so, struct sockaddr *nam)
1041 {
1042           int s;
1043 
1044           KASSERT(solocked(so));
1045           KASSERT(sotoinpcb(so) != NULL);
1046           KASSERT(nam != NULL);
1047 
1048           s = splsoftnet();
1049           inpcb_fetch_sockaddr(sotoinpcb(so), (struct sockaddr_in *)nam);
1050           splx(s);
1051 
1052           return 0;
1053 }
1054 
1055 static int
udp_rcvd(struct socket * so,int flags,struct lwp * l)1056 udp_rcvd(struct socket *so, int flags, struct lwp *l)
1057 {
1058           KASSERT(solocked(so));
1059 
1060           return EOPNOTSUPP;
1061 }
1062 
1063 static int
udp_recvoob(struct socket * so,struct mbuf * m,int flags)1064 udp_recvoob(struct socket *so, struct mbuf *m, int flags)
1065 {
1066           KASSERT(solocked(so));
1067 
1068           return EOPNOTSUPP;
1069 }
1070 
1071 int
udp_send(struct socket * so,struct mbuf * m,struct sockaddr * nam,struct mbuf * control,struct lwp * l)1072 udp_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
1073     struct mbuf *control, struct lwp *l)
1074 {
1075           struct inpcb *inp = sotoinpcb(so);
1076           int error = 0;
1077           struct in_addr laddr;                             /* XXX */
1078           int s;
1079 
1080           KASSERT(solocked(so));
1081           KASSERT(inp != NULL);
1082           KASSERT(m != NULL);
1083 
1084           memset(&laddr, 0, sizeof laddr);
1085 
1086           s = splsoftnet();
1087           if (nam) {
1088                     laddr = in4p_laddr(inp);                /* XXX */
1089                     if ((so->so_state & SS_ISCONNECTED) != 0) {
1090                               error = EISCONN;
1091                               goto die;
1092                     }
1093                     error = inpcb_connect(inp, (struct sockaddr_in *)nam, l);
1094                     if (error)
1095                               goto die;
1096           } else {
1097                     if ((so->so_state & SS_ISCONNECTED) == 0) {
1098                               error = ENOTCONN;
1099                               goto die;
1100                     }
1101           }
1102           error = udp_output(m, inp, control, l);
1103           m = NULL;
1104           control = NULL;
1105           if (nam) {
1106                     inpcb_disconnect(inp);
1107                     in4p_laddr(inp) = laddr;                /* XXX */
1108                     inpcb_set_state(inp, INP_BOUND);        /* XXX */
1109           }
1110   die:
1111           m_freem(m);
1112           m_freem(control);
1113 
1114           splx(s);
1115           return error;
1116 }
1117 
1118 static int
udp_sendoob(struct socket * so,struct mbuf * m,struct mbuf * control)1119 udp_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
1120 {
1121           KASSERT(solocked(so));
1122 
1123           m_freem(m);
1124           m_freem(control);
1125 
1126           return EOPNOTSUPP;
1127 }
1128 
1129 static int
udp_purgeif(struct socket * so,struct ifnet * ifp)1130 udp_purgeif(struct socket *so, struct ifnet *ifp)
1131 {
1132           int s;
1133 
1134           s = splsoftnet();
1135           mutex_enter(softnet_lock);
1136           inpcb_purgeif0(&udbtable, ifp);
1137 #ifdef NET_MPSAFE
1138           mutex_exit(softnet_lock);
1139 #endif
1140           in_purgeif(ifp);
1141 #ifdef NET_MPSAFE
1142           mutex_enter(softnet_lock);
1143 #endif
1144           inpcb_purgeif(&udbtable, ifp);
1145           mutex_exit(softnet_lock);
1146           splx(s);
1147 
1148           return 0;
1149 }
1150 
1151 static int
sysctl_net_inet_udp_stats(SYSCTLFN_ARGS)1152 sysctl_net_inet_udp_stats(SYSCTLFN_ARGS)
1153 {
1154 
1155           return (NETSTAT_SYSCTL(udpstat_percpu, UDP_NSTATS));
1156 }
1157 
1158 /*
1159  * Sysctl for udp variables.
1160  */
1161 static void
sysctl_net_inet_udp_setup(struct sysctllog ** clog)1162 sysctl_net_inet_udp_setup(struct sysctllog **clog)
1163 {
1164 
1165           sysctl_createv(clog, 0, NULL, NULL,
1166                            CTLFLAG_PERMANENT,
1167                            CTLTYPE_NODE, "inet", NULL,
1168                            NULL, 0, NULL, 0,
1169                            CTL_NET, PF_INET, CTL_EOL);
1170           sysctl_createv(clog, 0, NULL, NULL,
1171                            CTLFLAG_PERMANENT,
1172                            CTLTYPE_NODE, "udp",
1173                            SYSCTL_DESCR("UDPv4 related settings"),
1174                            NULL, 0, NULL, 0,
1175                            CTL_NET, PF_INET, IPPROTO_UDP, CTL_EOL);
1176 
1177           sysctl_createv(clog, 0, NULL, NULL,
1178                            CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1179                            CTLTYPE_INT, "checksum",
1180                            SYSCTL_DESCR("Compute UDP checksums"),
1181                            NULL, 0, &udpcksum, 0,
1182                            CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_CHECKSUM,
1183                            CTL_EOL);
1184           sysctl_createv(clog, 0, NULL, NULL,
1185                            CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1186                            CTLTYPE_INT, "sendspace",
1187                            SYSCTL_DESCR("Default UDP send buffer size"),
1188                            NULL, 0, &udp_sendspace, 0,
1189                            CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_SENDSPACE,
1190                            CTL_EOL);
1191           sysctl_createv(clog, 0, NULL, NULL,
1192                            CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1193                            CTLTYPE_INT, "recvspace",
1194                            SYSCTL_DESCR("Default UDP receive buffer size"),
1195                            NULL, 0, &udp_recvspace, 0,
1196                            CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_RECVSPACE,
1197                            CTL_EOL);
1198           sysctl_createv(clog, 0, NULL, NULL,
1199                            CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1200                            CTLTYPE_INT, "do_loopback_cksum",
1201                            SYSCTL_DESCR("Perform UDP checksum on loopback"),
1202                            NULL, 0, &udp_do_loopback_cksum, 0,
1203                            CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_LOOPBACKCKSUM,
1204                            CTL_EOL);
1205           sysctl_createv(clog, 0, NULL, NULL,
1206                            CTLFLAG_PERMANENT,
1207                            CTLTYPE_STRUCT, "pcblist",
1208                            SYSCTL_DESCR("UDP protocol control block list"),
1209                            sysctl_inpcblist, 0, &udbtable, 0,
1210                            CTL_NET, PF_INET, IPPROTO_UDP, CTL_CREATE,
1211                            CTL_EOL);
1212           sysctl_createv(clog, 0, NULL, NULL,
1213                            CTLFLAG_PERMANENT,
1214                            CTLTYPE_STRUCT, "stats",
1215                            SYSCTL_DESCR("UDP statistics"),
1216                            sysctl_net_inet_udp_stats, 0, NULL, 0,
1217                            CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_STATS,
1218                            CTL_EOL);
1219 }
1220 #endif
1221 
1222 void
udp_statinc(u_int stat)1223 udp_statinc(u_int stat)
1224 {
1225 
1226           KASSERT(stat < UDP_NSTATS);
1227           UDP_STATINC(stat);
1228 }
1229 
1230 #if defined(INET) && defined(IPSEC)
1231 /*
1232  * Handle ESP-in-UDP packets (RFC3948).
1233  *
1234  * We need to distinguish between ESP packets and IKE packets. We do so by
1235  * looking at the Non-ESP marker. If IKE, we process the UDP packet as usual.
1236  * Otherwise, ESP, we invoke IPsec.
1237  *
1238  * Returns:
1239  *     1 if the packet was processed
1240  *     0 if normal UDP processing should take place
1241  *    -1 if an error occurred and m was freed
1242  */
1243 static int
udp4_espinudp(struct mbuf ** mp,int off)1244 udp4_espinudp(struct mbuf **mp, int off)
1245 {
1246           const size_t skip = sizeof(struct udphdr);
1247           size_t len;
1248           uint8_t *data;
1249           size_t minlen;
1250           size_t iphdrlen;
1251           struct ip *ip;
1252           struct m_tag *tag;
1253           struct udphdr *udphdr;
1254           u_int16_t sport, dport;
1255           struct mbuf *m = *mp;
1256           uint32_t *marker;
1257 
1258           minlen = off + sizeof(struct esp);
1259           if (minlen > m->m_pkthdr.len)
1260                     minlen = m->m_pkthdr.len;
1261 
1262           if (m->m_len < minlen) {
1263                     if ((*mp = m_pullup(m, minlen)) == NULL) {
1264                               return -1; /* dropped */
1265                     }
1266                     m = *mp;
1267           }
1268 
1269           len = m->m_len - off;
1270           data = mtod(m, uint8_t *) + off;
1271 
1272           /* Ignore keepalive packets. */
1273           if ((len == 1) && (*data == 0xff)) {
1274                     m_freem(m);
1275                     *mp = NULL; /* avoid any further processing by caller */
1276                     return 1; /* consumed */
1277           }
1278 
1279           /* Handle Non-ESP marker (32bit). If zero, then IKE. */
1280           marker = (uint32_t *)data;
1281           if (len <= sizeof(uint32_t))
1282                     return 0; /* passthrough */
1283           if (marker[0] == 0)
1284                     return 0; /* passthrough */
1285 
1286           /*
1287            * Get the UDP ports. They are handled in network order
1288            * everywhere in the IPSEC_NAT_T code.
1289            */
1290           udphdr = (struct udphdr *)((char *)data - skip);
1291           sport = udphdr->uh_sport;
1292           dport = udphdr->uh_dport;
1293 
1294           /*
1295            * Remove the UDP header, plus a possible marker. IP header
1296            * length is iphdrlen.
1297            *
1298            * Before:
1299            *   <--- off --->
1300            *   +----+------+-----+
1301            *   | IP |  UDP | ESP |
1302            *   +----+------+-----+
1303            *        <-skip->
1304            * After:
1305            *          +----+-----+
1306            *          | IP | ESP |
1307            *          +----+-----+
1308            *   <-skip->
1309            */
1310           iphdrlen = off - sizeof(struct udphdr);
1311           memmove(mtod(m, char *) + skip, mtod(m, void *), iphdrlen);
1312           m_adj(m, skip);
1313 
1314           ip = mtod(m, struct ip *);
1315           ip->ip_len = htons(ntohs(ip->ip_len) - skip);
1316           ip->ip_p = IPPROTO_ESP;
1317 
1318           /*
1319            * We have modified the packet - it is now ESP, so we should not
1320            * return to UDP processing.
1321            *
1322            * Add a PACKET_TAG_IPSEC_NAT_T_PORTS tag to remember the source
1323            * UDP port. This is required if we want to select the right SPD
1324            * for multiple hosts behind same NAT.
1325            */
1326           if ((tag = m_tag_get(PACKET_TAG_IPSEC_NAT_T_PORTS,
1327               sizeof(sport) + sizeof(dport), M_DONTWAIT)) == NULL) {
1328                     m_freem(m);
1329                     *mp = NULL;
1330                     return -1;          /* dropped */
1331           }
1332           ((u_int16_t *)(tag + 1))[0] = sport;
1333           ((u_int16_t *)(tag + 1))[1] = dport;
1334           m_tag_prepend(m, tag);
1335 
1336           if (ipsec_used)
1337                     ipsec4_common_input(m, iphdrlen, IPPROTO_ESP);
1338           else
1339                     m_freem(m);
1340 
1341           /* We handled it, it shouldn't be handled by UDP */
1342           *mp = NULL; /* avoid free by caller ... */
1343           return 1;           /* consumed */
1344 }
1345 #endif
1346 
1347 PR_WRAP_USRREQS(udp)
1348 #define   udp_attach          udp_attach_wrapper
1349 #define   udp_detach          udp_detach_wrapper
1350 #define   udp_accept          udp_accept_wrapper
1351 #define   udp_bind  udp_bind_wrapper
1352 #define   udp_listen          udp_listen_wrapper
1353 #define   udp_connect         udp_connect_wrapper
1354 #define   udp_connect2        udp_connect2_wrapper
1355 #define   udp_disconnect      udp_disconnect_wrapper
1356 #define   udp_shutdown        udp_shutdown_wrapper
1357 #define   udp_abort udp_abort_wrapper
1358 #define   udp_ioctl udp_ioctl_wrapper
1359 #define   udp_stat  udp_stat_wrapper
1360 #define   udp_peeraddr        udp_peeraddr_wrapper
1361 #define   udp_sockaddr        udp_sockaddr_wrapper
1362 #define   udp_rcvd  udp_rcvd_wrapper
1363 #define   udp_recvoob         udp_recvoob_wrapper
1364 #define   udp_send  udp_send_wrapper
1365 #define   udp_sendoob         udp_sendoob_wrapper
1366 #define   udp_purgeif         udp_purgeif_wrapper
1367 
1368 const struct pr_usrreqs udp_usrreqs = {
1369           .pr_attach          = udp_attach,
1370           .pr_detach          = udp_detach,
1371           .pr_accept          = udp_accept,
1372           .pr_bind  = udp_bind,
1373           .pr_listen          = udp_listen,
1374           .pr_connect         = udp_connect,
1375           .pr_connect2        = udp_connect2,
1376           .pr_disconnect      = udp_disconnect,
1377           .pr_shutdown        = udp_shutdown,
1378           .pr_abort = udp_abort,
1379           .pr_ioctl = udp_ioctl,
1380           .pr_stat  = udp_stat,
1381           .pr_peeraddr        = udp_peeraddr,
1382           .pr_sockaddr        = udp_sockaddr,
1383           .pr_rcvd  = udp_rcvd,
1384           .pr_recvoob         = udp_recvoob,
1385           .pr_send  = udp_send,
1386           .pr_sendoob         = udp_sendoob,
1387           .pr_purgeif         = udp_purgeif,
1388 };
1389