PageRenderTime 62ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/bsd/sys/netinet/udp_usrreq.cc

https://gitlab.com/jforge/osv
C++ | 1552 lines | 1118 code | 143 blank | 291 comment | 267 complexity | 6e12aa0ae9f508511ef19c4e23af794a MD5 | raw file
Possible License(s): BSD-3-Clause, 0BSD, MPL-2.0-no-copyleft-exception
  1. /*-
  2. * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
  3. * The Regents of the University of California.
  4. * Copyright (c) 2008 Robert N. M. Watson
  5. * Copyright (c) 2010-2011 Juniper Networks, Inc.
  6. * All rights reserved.
  7. *
  8. * Portions of this software were developed by Robert N. M. Watson under
  9. * contract to Juniper Networks, Inc.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 4. Neither the name of the University nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. *
  35. * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
  36. */
  37. #include <sys/cdefs.h>
  38. #include <osv/initialize.hh>
  39. #include <bsd/porting/netport.h>
  40. #include <machine/in_cksum.h>
  41. #include <bsd/sys/sys/param.h>
  42. #include <bsd/sys/sys/domain.h>
  43. #include <bsd/sys/sys/eventhandler.h>
  44. #include <bsd/sys/sys/mbuf.h>
  45. #include <bsd/sys/sys/protosw.h>
  46. #include <bsd/sys/sys/socket.h>
  47. #include <bsd/sys/sys/socketvar.h>
  48. #include <bsd/sys/net/if.h>
  49. #include <bsd/sys/net/route.h>
  50. #include <bsd/sys/netinet/in.h>
  51. #include <bsd/sys/netinet/in_pcb.h>
  52. #include <bsd/sys/netinet/in_systm.h>
  53. #include <bsd/sys/netinet/in_var.h>
  54. #include <bsd/sys/netinet/ip.h>
  55. #ifdef INET6
  56. #include <netinet/ip6.h>
  57. #endif
  58. #include <bsd/sys/netinet/ip_icmp.h>
  59. #include <bsd/sys/netinet/icmp_var.h>
  60. #include <bsd/sys/netinet/ip_var.h>
  61. #include <bsd/sys/netinet/ip_options.h>
  62. #ifdef INET6
  63. #include <netinet6/ip6_var.h>
  64. #endif
  65. #include <bsd/sys/netinet/udp.h>
  66. #include <bsd/sys/netinet/udp_var.h>
  67. /*
  68. * UDP protocol implementation.
  69. * Per RFC 768, August, 1980.
  70. */
  71. /*
  72. * BSD 4.2 defaulted the udp checksum to be off. Turning off udp checksums
  73. * removes the only data integrity mechanism for packets and malformed
  74. * packets that would otherwise be discarded due to bad checksums, and may
  75. * cause problems (especially for NFS data blocks).
  76. */
  77. VNET_DEFINE(int, udp_cksum) = 1;
  78. SYSCTL_VNET_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
  79. &VNET_NAME(udp_cksum), 0, "compute udp checksum");
  80. int udp_log_in_vain = 0;
  81. SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
  82. &udp_log_in_vain, 0, "Log all incoming UDP packets");
  83. VNET_DEFINE(int, udp_blackhole) = 0;
  84. SYSCTL_VNET_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
  85. &VNET_NAME(udp_blackhole), 0,
  86. "Do not send port unreachables for refused connects");
  87. u_long udp_sendspace = 9216; /* really max datagram size */
  88. /* 40 1K datagrams */
  89. SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
  90. &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
  91. u_long udp_recvspace = 40 * (1024 +
  92. #ifdef INET6
  93. sizeof(struct bsd_sockaddr_in6)
  94. #else
  95. sizeof(struct bsd_sockaddr_in)
  96. #endif
  97. );
  98. SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
  99. &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
  100. VNET_DEFINE(struct inpcbhead, udb); /* from udp_var.h */
  101. VNET_DEFINE(struct inpcbinfo, udbinfo);
  102. static VNET_DEFINE(uma_zone_t, udpcb_zone);
  103. #define V_udpcb_zone VNET(udpcb_zone)
  104. #ifndef UDBHASHSIZE
  105. #define UDBHASHSIZE 128
  106. #endif
  107. VNET_DEFINE(struct udpstat, udpstat); /* from udp_var.h */
  108. SYSCTL_VNET_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW,
  109. &VNET_NAME(udpstat), udpstat,
  110. "UDP statistics (struct udpstat, netinet/udp_var.h)");
  111. #ifdef INET
  112. static void udp_detach(struct socket *so);
  113. static int udp_output(struct inpcb *, struct mbuf *, struct bsd_sockaddr *,
  114. struct mbuf *, struct thread *);
  115. #endif
  116. #ifdef IPSEC
  117. #ifdef IPSEC_NAT_T
  118. #define UF_ESPINUDP_ALL (UF_ESPINUDP_NON_IKE|UF_ESPINUDP)
  119. #ifdef INET
  120. static struct mbuf *udp4_espdecap(struct inpcb *, struct mbuf *, int);
  121. #endif
  122. #endif /* IPSEC_NAT_T */
  123. #endif /* IPSEC */
  124. static void
  125. udp_zone_change(void *tag)
  126. {
  127. // FIXME: uma_zone_set_max(V_udbinfo.ipi_zone, maxsockets);
  128. uma_zone_set_max(V_udpcb_zone, maxsockets);
  129. }
  130. void
  131. udp_init(void)
  132. {
  133. in_pcbinfo_init(&V_udbinfo, "udp", &V_udb, UDBHASHSIZE, UDBHASHSIZE,
  134. IPI_HASHFIELDS_2TUPLE);
  135. V_udpcb_zone = uma_zcreate("udpcb", sizeof(struct udpcb),
  136. NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
  137. uma_zone_set_max(V_udpcb_zone, maxsockets);
  138. EVENTHANDLER_REGISTER(maxsockets_change, (void *)udp_zone_change, NULL,
  139. EVENTHANDLER_PRI_ANY);
  140. }
  141. /*
  142. * Kernel module interface for updating udpstat. The argument is an index
  143. * into udpstat treated as an array of u_long. While this encodes the
  144. * general layout of udpstat into the caller, it doesn't encode its location,
  145. * so that future changes to add, for example, per-CPU stats support won't
  146. * cause binary compatibility problems for kernel modules.
  147. */
  148. void
  149. kmod_udpstat_inc(int statnum)
  150. {
  151. (*((u_long *)&V_udpstat + statnum))++;
  152. }
  153. int
  154. udp_newudpcb(struct inpcb *inp)
  155. {
  156. struct udpcb *up;
  157. up = (udpcb *)uma_zalloc(V_udpcb_zone, M_NOWAIT | M_ZERO);
  158. if (up == NULL)
  159. return (ENOBUFS);
  160. inp->inp_ppcb = up;
  161. return (0);
  162. }
  163. void
  164. udp_discardcb(struct udpcb *up)
  165. {
  166. uma_zfree(V_udpcb_zone, up);
  167. }
  168. #ifdef INET
  169. /*
  170. * Subroutine of udp_input(), which appends the provided mbuf chain to the
  171. * passed pcb/socket. The caller must provide a bsd_sockaddr_in via udp_in that
  172. * contains the source address. If the socket ends up being an IPv6 socket,
  173. * udp_append() will convert to a bsd_sockaddr_in6 before passing the address
  174. * into the socket code.
  175. */
  176. static void
  177. udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off,
  178. struct bsd_sockaddr_in *udp_in)
  179. {
  180. struct bsd_sockaddr *append_sa;
  181. struct socket *so;
  182. struct mbuf *opts = 0;
  183. #ifdef INET6
  184. struct bsd_sockaddr_in6 udp_in6;
  185. #endif
  186. struct udpcb *up;
  187. INP_LOCK_ASSERT(inp);
  188. /*
  189. * Engage the tunneling protocol.
  190. */
  191. up = intoudpcb(inp);
  192. if (up->u_tun_func != NULL) {
  193. (*up->u_tun_func)(n, off, inp);
  194. return;
  195. }
  196. if (n == NULL)
  197. return;
  198. off += sizeof(struct udphdr);
  199. #ifdef IPSEC
  200. /* Check AH/ESP integrity. */
  201. if (ipsec4_in_reject(n, inp)) {
  202. m_freem(n);
  203. V_ipsec4stat.in_polvio++;
  204. return;
  205. }
  206. #ifdef IPSEC_NAT_T
  207. up = intoudpcb(inp);
  208. KASSERT(up != NULL, ("%s: udpcb NULL", __func__));
  209. if (up->u_flags & UF_ESPINUDP_ALL) { /* IPSec UDP encaps. */
  210. n = udp4_espdecap(inp, n, off);
  211. if (n == NULL) /* Consumed. */
  212. return;
  213. }
  214. #endif /* IPSEC_NAT_T */
  215. #endif /* IPSEC */
  216. #ifdef MAC
  217. if (mac_inpcb_check_deliver(inp, n) != 0) {
  218. m_freem(n);
  219. return;
  220. }
  221. #endif /* MAC */
  222. if (inp->inp_flags & INP_CONTROLOPTS ||
  223. inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
  224. #ifdef INET6
  225. if (inp->inp_vflag & INP_IPV6)
  226. (void)ip6_savecontrol_v4(inp, n, &opts, NULL);
  227. else
  228. #endif /* INET6 */
  229. ip_savecontrol(inp, &opts, ip, n);
  230. }
  231. #ifdef INET6
  232. if (inp->inp_vflag & INP_IPV6) {
  233. bzero(&udp_in6, sizeof(udp_in6));
  234. udp_in6.sin6_len = sizeof(udp_in6);
  235. udp_in6.sin6_family = AF_INET6;
  236. in6_sin_2_v4mapsin6(udp_in, &udp_in6);
  237. append_sa = (struct bsd_sockaddr *)&udp_in6;
  238. } else
  239. #endif /* INET6 */
  240. append_sa = (struct bsd_sockaddr *)udp_in;
  241. m_adj(n, off);
  242. so = inp->inp_socket;
  243. SOCK_LOCK_ASSERT(so);
  244. if (sbappendaddr_locked(so, &so->so_rcv, append_sa, n, opts) == 0) {
  245. m_freem(n);
  246. if (opts)
  247. m_freem(opts);
  248. UDPSTAT_INC(udps_fullsock);
  249. } else
  250. sorwakeup_locked(so);
  251. }
  252. void
  253. udp_input(struct mbuf *m, int off)
  254. {
  255. int iphlen = off;
  256. struct ip *ip;
  257. struct udphdr *uh;
  258. struct ifnet *ifp;
  259. struct inpcb *inp;
  260. int len;
  261. struct ip save_ip;
  262. struct bsd_sockaddr_in udp_in;
  263. struct m_tag *fwd_tag;
  264. ifp = m->M_dat.MH.MH_pkthdr.rcvif;
  265. UDPSTAT_INC(udps_ipackets);
  266. /*
  267. * Strip IP options, if any; should skip this, make available to
  268. * user, and use on returned packets, but we don't yet have a way to
  269. * check the checksum with options still present.
  270. */
  271. if (iphlen > sizeof (struct ip)) {
  272. ip_stripoptions(m, (struct mbuf *)0);
  273. iphlen = sizeof(struct ip);
  274. }
  275. /*
  276. * Get IP and UDP header together in first mbuf.
  277. */
  278. ip = mtod(m, struct ip *);
  279. if (m->m_hdr.mh_len < iphlen + sizeof(struct udphdr)) {
  280. if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
  281. UDPSTAT_INC(udps_hdrops);
  282. return;
  283. }
  284. ip = mtod(m, struct ip *);
  285. }
  286. uh = (struct udphdr *)((caddr_t)ip + iphlen);
  287. /*
  288. * Destination port of 0 is illegal, based on RFC768.
  289. */
  290. if (uh->uh_dport == 0)
  291. goto badunlocked;
  292. /*
  293. * Construct bsd_sockaddr format source address. Stuff source address
  294. * and datagram in user buffer.
  295. */
  296. bzero(&udp_in, sizeof(udp_in));
  297. udp_in.sin_len = sizeof(udp_in);
  298. udp_in.sin_family = AF_INET;
  299. udp_in.sin_port = uh->uh_sport;
  300. udp_in.sin_addr = ip->ip_src;
  301. /*
  302. * Make mbuf data length reflect UDP length. If not enough data to
  303. * reflect UDP length, drop.
  304. */
  305. len = ntohs((u_short)uh->uh_ulen);
  306. if (ip->ip_len != len) {
  307. if (len > ip->ip_len || len < sizeof(struct udphdr)) {
  308. UDPSTAT_INC(udps_badlen);
  309. goto badunlocked;
  310. }
  311. m_adj(m, len - ip->ip_len);
  312. /* ip->ip_len = len; */
  313. }
  314. /*
  315. * Save a copy of the IP header in case we want restore it for
  316. * sending an ICMP error message in response.
  317. */
  318. if (!V_udp_blackhole)
  319. save_ip = *ip;
  320. else
  321. memset(&save_ip, 0, sizeof(save_ip));
  322. /*
  323. * Checksum extended UDP header and data.
  324. */
  325. if (uh->uh_sum) {
  326. u_short uh_sum;
  327. if (m->M_dat.MH.MH_pkthdr.csum_flags & CSUM_DATA_VALID) {
  328. if (m->M_dat.MH.MH_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
  329. uh_sum = m->M_dat.MH.MH_pkthdr.csum_data;
  330. else
  331. uh_sum = in_pseudo(ip->ip_src.s_addr,
  332. ip->ip_dst.s_addr, htonl((u_short)len +
  333. m->M_dat.MH.MH_pkthdr.csum_data + IPPROTO_UDP));
  334. uh_sum ^= 0xffff;
  335. } else {
  336. char b[9];
  337. bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
  338. bzero(((struct ipovly *)ip)->ih_x1, 9);
  339. ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
  340. uh_sum = in_cksum(m, len + sizeof (struct ip));
  341. bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
  342. }
  343. if (uh_sum) {
  344. UDPSTAT_INC(udps_badsum);
  345. m_freem(m);
  346. return;
  347. }
  348. } else
  349. UDPSTAT_INC(udps_nosum);
  350. if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
  351. in_broadcast(ip->ip_dst, ifp)) {
  352. struct inpcb *last;
  353. struct ip_moptions *imo;
  354. INP_INFO_WLOCK(&V_udbinfo);
  355. last = NULL;
  356. LIST_FOREACH(inp, &V_udb, inp_list) {
  357. if (inp->inp_lport != uh->uh_dport)
  358. continue;
  359. #ifdef INET6
  360. if ((inp->inp_vflag & INP_IPV4) == 0)
  361. continue;
  362. #endif
  363. if (inp->inp_laddr.s_addr != INADDR_ANY &&
  364. inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
  365. continue;
  366. if (inp->inp_faddr.s_addr != INADDR_ANY &&
  367. inp->inp_faddr.s_addr != ip->ip_src.s_addr)
  368. continue;
  369. if (inp->inp_fport != 0 &&
  370. inp->inp_fport != uh->uh_sport)
  371. continue;
  372. INP_LOCK(inp);
  373. /*
  374. * XXXRW: Because we weren't holding either the inpcb
  375. * or the hash lock when we checked for a match
  376. * before, we should probably recheck now that the
  377. * inpcb lock is held.
  378. */
  379. /*
  380. * Handle socket delivery policy for any-source
  381. * and source-specific multicast. [RFC3678]
  382. */
  383. imo = inp->inp_moptions;
  384. if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
  385. struct bsd_sockaddr_in group;
  386. int blocked;
  387. if (imo == NULL) {
  388. INP_UNLOCK(inp);
  389. continue;
  390. }
  391. bzero(&group, sizeof(struct bsd_sockaddr_in));
  392. group.sin_len = sizeof(struct bsd_sockaddr_in);
  393. group.sin_family = AF_INET;
  394. group.sin_addr = ip->ip_dst;
  395. blocked = imo_multi_filter(imo, ifp,
  396. (struct bsd_sockaddr *)&group,
  397. (struct bsd_sockaddr *)&udp_in);
  398. if (blocked != MCAST_PASS) {
  399. if (blocked == MCAST_NOTGMEMBER)
  400. IPSTAT_INC(ips_notmember);
  401. if (blocked == MCAST_NOTSMEMBER ||
  402. blocked == MCAST_MUTED)
  403. UDPSTAT_INC(udps_filtermcast);
  404. INP_UNLOCK(inp);
  405. continue;
  406. }
  407. }
  408. if (last != NULL) {
  409. struct mbuf *n;
  410. n = m_copy(m, 0, M_COPYALL);
  411. udp_append(last, ip, n, iphlen, &udp_in);
  412. INP_UNLOCK(last);
  413. }
  414. last = inp;
  415. /*
  416. * Don't look for additional matches if this one does
  417. * not have either the SO_REUSEPORT or SO_REUSEADDR
  418. * socket options set. This heuristic avoids
  419. * searching through all pcbs in the common case of a
  420. * non-shared port. It assumes that an application
  421. * will never clear these options after setting them.
  422. */
  423. if ((last->inp_socket->so_options &
  424. (SO_REUSEPORT|SO_REUSEADDR)) == 0)
  425. break;
  426. }
  427. if (last == NULL) {
  428. /*
  429. * No matching pcb found; discard datagram. (No need
  430. * to send an ICMP Port Unreachable for a broadcast
  431. * or multicast datgram.)
  432. */
  433. UDPSTAT_INC(udps_noportbcast);
  434. if (inp)
  435. INP_UNLOCK(inp);
  436. INP_INFO_WUNLOCK(&V_udbinfo);
  437. goto badunlocked;
  438. }
  439. udp_append(last, ip, m, iphlen, &udp_in);
  440. INP_UNLOCK(last);
  441. INP_INFO_WUNLOCK(&V_udbinfo);
  442. return;
  443. }
  444. /*
  445. * Locate pcb for datagram.
  446. */
  447. /*
  448. * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
  449. */
  450. if ((m->m_hdr.mh_flags & M_IP_NEXTHOP) &&
  451. (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
  452. struct bsd_sockaddr_in *next_hop;
  453. next_hop = (struct bsd_sockaddr_in *)(fwd_tag + 1);
  454. /*
  455. * Transparently forwarded. Pretend to be the destination.
  456. * Already got one like this?
  457. */
  458. inp = in_pcblookup_mbuf(&V_udbinfo, ip->ip_src, uh->uh_sport,
  459. ip->ip_dst, uh->uh_dport, INPLOOKUP_LOCKPCB, ifp, m);
  460. if (!inp) {
  461. /*
  462. * It's new. Try to find the ambushing socket.
  463. * Because we've rewritten the destination address,
  464. * any hardware-generated hash is ignored.
  465. */
  466. inp = in_pcblookup(&V_udbinfo, ip->ip_src,
  467. uh->uh_sport, next_hop->sin_addr,
  468. next_hop->sin_port ? htons(next_hop->sin_port) :
  469. uh->uh_dport, INPLOOKUP_WILDCARD |
  470. INPLOOKUP_LOCKPCB, ifp);
  471. }
  472. /* Remove the tag from the packet. We don't need it anymore. */
  473. m_tag_delete(m, fwd_tag);
  474. m->m_hdr.mh_flags &= ~M_IP_NEXTHOP;
  475. } else
  476. inp = in_pcblookup_mbuf(&V_udbinfo, ip->ip_src, uh->uh_sport,
  477. ip->ip_dst, uh->uh_dport, INPLOOKUP_WILDCARD |
  478. INPLOOKUP_LOCKPCB, ifp, m);
  479. if (inp == NULL) {
  480. if (udp_log_in_vain) {
  481. char buf[4*sizeof "123"];
  482. strcpy(buf, inet_ntoa(ip->ip_dst));
  483. bsd_log(LOG_INFO,
  484. "Connection attempt to UDP %s:%d from %s:%d\n",
  485. buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
  486. ntohs(uh->uh_sport));
  487. }
  488. UDPSTAT_INC(udps_noport);
  489. if (m->m_hdr.mh_flags & (M_BCAST | M_MCAST)) {
  490. UDPSTAT_INC(udps_noportbcast);
  491. goto badunlocked;
  492. }
  493. if (V_udp_blackhole)
  494. goto badunlocked;
  495. if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
  496. goto badunlocked;
  497. *ip = save_ip;
  498. ip->ip_len += iphlen;
  499. icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
  500. return;
  501. }
  502. /*
  503. * Check the minimum TTL for socket.
  504. */
  505. INP_LOCK_ASSERT(inp);
  506. if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) {
  507. INP_UNLOCK(inp);
  508. m_freem(m);
  509. return;
  510. }
  511. udp_append(inp, ip, m, iphlen, &udp_in);
  512. INP_UNLOCK(inp);
  513. return;
  514. badunlocked:
  515. m_freem(m);
  516. }
  517. #endif /* INET */
  518. /*
  519. * Notify a udp user of an asynchronous error; just wake up so that they can
  520. * collect error status.
  521. */
  522. struct inpcb *
  523. udp_notify(struct inpcb *inp, int errval)
  524. {
  525. /*
  526. * While udp_ctlinput() always calls udp_notify() with a read lock
  527. * when invoking it directly, in_pcbnotifyall() currently uses write
  528. * locks due to sharing code with TCP. For now, accept either a read
  529. * or a write lock, but a read lock is sufficient.
  530. */
  531. INP_LOCK_ASSERT(inp);
  532. inp->inp_socket->so_error = errval;
  533. sorwakeup(inp->inp_socket);
  534. sowwakeup(inp->inp_socket);
  535. return (inp);
  536. }
  537. #ifdef INET
  538. void
  539. udp_ctlinput(int cmd, struct bsd_sockaddr *sa, void *vip)
  540. {
  541. struct ip *ip = (struct ip *)vip;
  542. struct udphdr *uh;
  543. struct in_addr faddr;
  544. struct inpcb *inp;
  545. faddr = ((struct bsd_sockaddr_in *)sa)->sin_addr;
  546. if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
  547. return;
  548. /*
  549. * Redirects don't need to be handled up here.
  550. */
  551. if (PRC_IS_REDIRECT(cmd))
  552. return;
  553. /*
  554. * Hostdead is ugly because it goes linearly through all PCBs.
  555. *
  556. * XXX: We never get this from ICMP, otherwise it makes an excellent
  557. * DoS attack on machines with many connections.
  558. */
  559. if (cmd == PRC_HOSTDEAD)
  560. ip = NULL;
  561. else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
  562. return;
  563. if (ip != NULL) {
  564. uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
  565. inp = in_pcblookup(&V_udbinfo, faddr, uh->uh_dport,
  566. ip->ip_src, uh->uh_sport, INPLOOKUP_LOCKPCB, NULL);
  567. if (inp != NULL) {
  568. INP_LOCK_ASSERT(inp);
  569. if (inp->inp_socket != NULL) {
  570. udp_notify(inp, inetctlerrmap[cmd]);
  571. }
  572. INP_UNLOCK(inp);
  573. }
  574. } else
  575. in_pcbnotifyall(&V_udbinfo, faddr, inetctlerrmap[cmd],
  576. udp_notify);
  577. }
  578. #endif /* INET */
  579. #if 0
  580. static int
  581. udp_pcblist(SYSCTL_HANDLER_ARGS)
  582. {
  583. int error, i, n;
  584. struct inpcb *inp, **inp_list;
  585. inp_gen_t gencnt;
  586. struct xinpgen xig;
  587. /*
  588. * The process of preparing the PCB list is too time-consuming and
  589. * resource-intensive to repeat twice on every request.
  590. */
  591. if (req->oldptr == 0) {
  592. n = V_udbinfo.ipi_count;
  593. n += imax(n / 8, 10);
  594. req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
  595. return (0);
  596. }
  597. if (req->newptr != 0)
  598. return (EPERM);
  599. /*
  600. * OK, now we're committed to doing something.
  601. */
  602. INP_INFO_RLOCK(&V_udbinfo);
  603. gencnt = V_udbinfo.ipi_gencnt;
  604. n = V_udbinfo.ipi_count;
  605. INP_INFO_RUNLOCK(&V_udbinfo);
  606. error = sysctl_wire_old_buffer(req, 2 * (sizeof xig)
  607. + n * sizeof(struct xinpcb));
  608. if (error != 0)
  609. return (error);
  610. xig.xig_len = sizeof xig;
  611. xig.xig_count = n;
  612. xig.xig_gen = gencnt;
  613. xig.xig_sogen = so_gencnt;
  614. error = SYSCTL_OUT(req, &xig, sizeof xig);
  615. if (error)
  616. return (error);
  617. inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
  618. if (inp_list == 0)
  619. return (ENOMEM);
  620. INP_INFO_RLOCK(&V_udbinfo);
  621. for (inp = LIST_FIRST(V_udbinfo.ipi_listhead), i = 0; inp && i < n;
  622. inp = LIST_NEXT(inp, inp_list)) {
  623. INP_LOCK(inp);
  624. if (inp->inp_gencnt <= gencnt &&
  625. cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
  626. in_pcbref(inp);
  627. inp_list[i++] = inp;
  628. }
  629. INP_UNLOCK(inp);
  630. }
  631. INP_INFO_RUNLOCK(&V_udbinfo);
  632. n = i;
  633. error = 0;
  634. for (i = 0; i < n; i++) {
  635. inp = inp_list[i];
  636. INP_LOCK(inp);
  637. if (inp->inp_gencnt <= gencnt) {
  638. struct xinpcb xi;
  639. bzero(&xi, sizeof(xi));
  640. xi.xi_len = sizeof xi;
  641. /* XXX should avoid extra copy */
  642. bcopy(inp, &xi.xi_inp, sizeof *inp);
  643. if (inp->inp_socket)
  644. sotoxsocket(inp->inp_socket, &xi.xi_socket);
  645. xi.xi_inp.inp_gencnt = inp->inp_gencnt;
  646. INP_UNLOCK(inp);
  647. error = SYSCTL_OUT(req, &xi, sizeof xi);
  648. } else
  649. INP_UNLOCK(inp);
  650. }
  651. INP_INFO_WLOCK(&V_udbinfo);
  652. for (i = 0; i < n; i++) {
  653. inp = inp_list[i];
  654. INP_LOCK(inp);
  655. if (!in_pcbrele_rlocked(inp))
  656. INP_UNLOCK(inp);
  657. }
  658. INP_INFO_WUNLOCK(&V_udbinfo);
  659. if (!error) {
  660. /*
  661. * Give the user an updated idea of our state. If the
  662. * generation differs from what we told her before, she knows
  663. * that something happened while we were processing this
  664. * request, and it might be necessary to retry.
  665. */
  666. INP_INFO_RLOCK(&V_udbinfo);
  667. xig.xig_gen = V_udbinfo.ipi_gencnt;
  668. xig.xig_sogen = so_gencnt;
  669. xig.xig_count = V_udbinfo.ipi_count;
  670. INP_INFO_RUNLOCK(&V_udbinfo);
  671. error = SYSCTL_OUT(req, &xig, sizeof xig);
  672. }
  673. free(inp_list, M_TEMP);
  674. return (error);
  675. }
  676. SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist,
  677. CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0,
  678. udp_pcblist, "S,xinpcb", "List of active UDP sockets");
  679. #ifdef INET
  680. static int
  681. udp_getcred(SYSCTL_HANDLER_ARGS)
  682. {
  683. struct xucred xuc;
  684. struct bsd_sockaddr_in addrs[2];
  685. struct inpcb *inp;
  686. int error;
  687. error = priv_check(req->td, PRIV_NETINET_GETCRED);
  688. if (error)
  689. return (error);
  690. error = SYSCTL_IN(req, addrs, sizeof(addrs));
  691. if (error)
  692. return (error);
  693. inp = in_pcblookup(&V_udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
  694. addrs[0].sin_addr, addrs[0].sin_port,
  695. INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
  696. if (inp != NULL) {
  697. INP_LOCK_ASSERT(inp);
  698. if (inp->inp_socket == NULL)
  699. error = ENOENT;
  700. if (error == 0)
  701. error = cr_canseeinpcb(req->td->td_ucred, inp);
  702. if (error == 0)
  703. cru2x(inp->inp_cred, &xuc);
  704. INP_UNLOCK(inp);
  705. } else
  706. error = ENOENT;
  707. if (error == 0)
  708. error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
  709. return (error);
  710. }
  711. SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
  712. CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
  713. udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
  714. #endif /* INET */
  715. #endif
  716. int
  717. udp_ctloutput(struct socket *so, struct sockopt *sopt)
  718. {
  719. int error = 0, optval;
  720. struct inpcb *inp;
  721. #ifdef IPSEC_NAT_T
  722. struct udpcb *up;
  723. #endif
  724. inp = sotoinpcb(so);
  725. KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
  726. INP_LOCK(inp);
  727. if (sopt->sopt_level != IPPROTO_UDP) {
  728. #ifdef INET6
  729. if (INP_CHECK_SOCKAF(so, AF_INET6)) {
  730. INP_UNLOCK(inp);
  731. error = ip6_ctloutput(so, sopt);
  732. }
  733. #endif
  734. #if defined(INET) && defined(INET6)
  735. else
  736. #endif
  737. #ifdef INET
  738. {
  739. INP_UNLOCK(inp);
  740. error = ip_ctloutput(so, sopt);
  741. }
  742. #endif
  743. return (error);
  744. }
  745. switch (sopt->sopt_dir) {
  746. case SOPT_SET:
  747. switch (sopt->sopt_name) {
  748. case UDP_ENCAP:
  749. INP_UNLOCK(inp);
  750. error = sooptcopyin(sopt, &optval, sizeof optval,
  751. sizeof optval);
  752. if (error)
  753. break;
  754. inp = sotoinpcb(so);
  755. KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
  756. INP_LOCK(inp);
  757. #ifdef IPSEC_NAT_T
  758. up = intoudpcb(inp);
  759. KASSERT(up != NULL, ("%s: up == NULL", __func__));
  760. #endif
  761. switch (optval) {
  762. case 0:
  763. /* Clear all UDP encap. */
  764. #ifdef IPSEC_NAT_T
  765. up->u_flags &= ~UF_ESPINUDP_ALL;
  766. #endif
  767. break;
  768. #ifdef IPSEC_NAT_T
  769. case UDP_ENCAP_ESPINUDP:
  770. case UDP_ENCAP_ESPINUDP_NON_IKE:
  771. up->u_flags &= ~UF_ESPINUDP_ALL;
  772. if (optval == UDP_ENCAP_ESPINUDP)
  773. up->u_flags |= UF_ESPINUDP;
  774. else if (optval == UDP_ENCAP_ESPINUDP_NON_IKE)
  775. up->u_flags |= UF_ESPINUDP_NON_IKE;
  776. break;
  777. #endif
  778. default:
  779. error = EINVAL;
  780. break;
  781. }
  782. INP_UNLOCK(inp);
  783. break;
  784. default:
  785. INP_UNLOCK(inp);
  786. error = ENOPROTOOPT;
  787. break;
  788. }
  789. break;
  790. case SOPT_GET:
  791. switch (sopt->sopt_name) {
  792. #ifdef IPSEC_NAT_T
  793. case UDP_ENCAP:
  794. up = intoudpcb(inp);
  795. KASSERT(up != NULL, ("%s: up == NULL", __func__));
  796. optval = up->u_flags & UF_ESPINUDP_ALL;
  797. INP_UNLOCK(inp);
  798. error = sooptcopyout(sopt, &optval, sizeof optval);
  799. break;
  800. #endif
  801. default:
  802. INP_UNLOCK(inp);
  803. error = ENOPROTOOPT;
  804. break;
  805. }
  806. break;
  807. }
  808. return (error);
  809. }
  810. #ifdef INET
  811. #define UH_WLOCKED 2
  812. #define UH_RLOCKED 1
  813. #define UH_UNLOCKED 0
  814. static int
  815. udp_output(struct inpcb *inp, struct mbuf *m, struct bsd_sockaddr *addr,
  816. struct mbuf *control, struct thread *td)
  817. {
  818. struct udpiphdr *ui;
  819. int len = m->M_dat.MH.MH_pkthdr.len;
  820. struct in_addr faddr, laddr;
  821. struct cmsghdr *cm;
  822. struct bsd_sockaddr_in *sin, src;
  823. int error = 0;
  824. int ipflags;
  825. u_short fport, lport;
  826. int unlock_udbinfo;
  827. u_char tos;
  828. /*
  829. * udp_output() may need to temporarily bind or connect the current
  830. * inpcb. As such, we don't know up front whether we will need the
  831. * pcbinfo lock or not. Do any work to decide what is needed up
  832. * front before acquiring any locks.
  833. */
  834. if (len + sizeof(struct udpiphdr) > IP_MAXSEGMENT) {
  835. if (control)
  836. m_freem(control);
  837. m_freem(m);
  838. return (EMSGSIZE);
  839. }
  840. src.sin_family = 0;
  841. INP_LOCK(inp);
  842. tos = inp->inp_ip_tos;
  843. if (control != NULL) {
  844. /*
  845. * XXX: Currently, we assume all the optional information is
  846. * stored in a single mbuf.
  847. */
  848. if (control->m_hdr.mh_next) {
  849. INP_UNLOCK(inp);
  850. m_freem(control);
  851. m_freem(m);
  852. return (EINVAL);
  853. }
  854. for (; control->m_hdr.mh_len > 0;
  855. control->m_hdr.mh_data += CMSG_ALIGN(cm->cmsg_len),
  856. control->m_hdr.mh_len -= CMSG_ALIGN(cm->cmsg_len)) {
  857. cm = mtod(control, struct cmsghdr *);
  858. if (control->m_hdr.mh_len < sizeof(*cm) || cm->cmsg_len == 0
  859. || cm->cmsg_len > control->m_hdr.mh_len) {
  860. error = EINVAL;
  861. break;
  862. }
  863. if (cm->cmsg_level != IPPROTO_IP)
  864. continue;
  865. switch (cm->cmsg_type) {
  866. case IP_SENDSRCADDR:
  867. if (cm->cmsg_len !=
  868. CMSG_LEN(sizeof(struct in_addr))) {
  869. error = EINVAL;
  870. break;
  871. }
  872. bzero(&src, sizeof(src));
  873. src.sin_family = AF_INET;
  874. src.sin_len = sizeof(src);
  875. src.sin_port = inp->inp_lport;
  876. src.sin_addr =
  877. *(struct in_addr *)CMSG_DATA(cm);
  878. break;
  879. case IP_TOS:
  880. if (cm->cmsg_len != CMSG_LEN(sizeof(u_char))) {
  881. error = EINVAL;
  882. break;
  883. }
  884. tos = *(u_char *)CMSG_DATA(cm);
  885. break;
  886. default:
  887. error = ENOPROTOOPT;
  888. break;
  889. }
  890. if (error)
  891. break;
  892. }
  893. m_freem(control);
  894. }
  895. if (error) {
  896. INP_UNLOCK(inp);
  897. m_freem(m);
  898. return (error);
  899. }
  900. /*
  901. * Depending on whether or not the application has bound or connected
  902. * the socket, we may have to do varying levels of work. The optimal
  903. * case is for a connected UDP socket, as a global lock isn't
  904. * required at all.
  905. *
  906. * In order to decide which we need, we require stability of the
  907. * inpcb binding, which we ensure by acquiring a read lock on the
  908. * inpcb. This doesn't strictly follow the lock order, so we play
  909. * the trylock and retry game; note that we may end up with more
  910. * conservative locks than required the second time around, so later
  911. * assertions have to accept that. Further analysis of the number of
  912. * misses under contention is required.
  913. *
  914. * XXXRW: Check that hash locking update here is correct.
  915. */
  916. sin = (struct bsd_sockaddr_in *)addr;
  917. if (sin != NULL &&
  918. (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0)) {
  919. INP_HASH_WLOCK(&V_udbinfo);
  920. unlock_udbinfo = UH_WLOCKED;
  921. } else if ((sin != NULL && (
  922. (sin->sin_addr.s_addr == INADDR_ANY) ||
  923. (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
  924. (inp->inp_laddr.s_addr == INADDR_ANY) ||
  925. (inp->inp_lport == 0))) ||
  926. (src.sin_family == AF_INET)) {
  927. INP_HASH_RLOCK(&V_udbinfo);
  928. unlock_udbinfo = UH_RLOCKED;
  929. } else
  930. unlock_udbinfo = UH_UNLOCKED;
  931. /*
  932. * If the IP_SENDSRCADDR control message was specified, override the
  933. * source address for this datagram. Its use is invalidated if the
  934. * address thus specified is incomplete or clobbers other inpcbs.
  935. */
  936. laddr = inp->inp_laddr;
  937. lport = inp->inp_lport;
  938. if (src.sin_family == AF_INET) {
  939. INP_HASH_LOCK_ASSERT(&V_udbinfo);
  940. if ((lport == 0) ||
  941. (laddr.s_addr == INADDR_ANY &&
  942. src.sin_addr.s_addr == INADDR_ANY)) {
  943. error = EINVAL;
  944. goto release;
  945. }
  946. error = in_pcbbind_setup(inp, (struct bsd_sockaddr *)&src,
  947. &laddr.s_addr, &lport, 0);
  948. if (error)
  949. goto release;
  950. }
  951. /*
  952. * If a UDP socket has been connected, then a local address/port will
  953. * have been selected and bound.
  954. *
  955. * If a UDP socket has not been connected to, then an explicit
  956. * destination address must be used, in which case a local
  957. * address/port may not have been selected and bound.
  958. */
  959. if (sin != NULL) {
  960. INP_LOCK_ASSERT(inp);
  961. if (inp->inp_faddr.s_addr != INADDR_ANY) {
  962. error = EISCONN;
  963. goto release;
  964. }
  965. /*
  966. * If a local address or port hasn't yet been selected, or if
  967. * the destination address needs to be rewritten due to using
  968. * a special INADDR_ constant, invoke in_pcbconnect_setup()
  969. * to do the heavy lifting. Once a port is selected, we
  970. * commit the binding back to the socket; we also commit the
  971. * binding of the address if in jail.
  972. *
  973. * If we already have a valid binding and we're not
  974. * requesting a destination address rewrite, use a fast path.
  975. */
  976. if (inp->inp_laddr.s_addr == INADDR_ANY ||
  977. inp->inp_lport == 0 ||
  978. sin->sin_addr.s_addr == INADDR_ANY ||
  979. sin->sin_addr.s_addr == INADDR_BROADCAST) {
  980. INP_HASH_LOCK_ASSERT(&V_udbinfo);
  981. error = in_pcbconnect_setup(inp, addr, &laddr.s_addr,
  982. &lport, &faddr.s_addr, &fport, NULL, 0);
  983. if (error)
  984. goto release;
  985. /*
  986. * XXXRW: Why not commit the port if the address is
  987. * !INADDR_ANY?
  988. */
  989. /* Commit the local port if newly assigned. */
  990. if (inp->inp_laddr.s_addr == INADDR_ANY &&
  991. inp->inp_lport == 0) {
  992. INP_LOCK_ASSERT(inp);
  993. INP_HASH_WLOCK_ASSERT(&V_udbinfo);
  994. inp->inp_laddr = laddr;
  995. inp->inp_lport = lport;
  996. if (in_pcbinshash(inp) != 0) {
  997. inp->inp_lport = 0;
  998. error = EAGAIN;
  999. goto release;
  1000. }
  1001. inp->inp_flags |= INP_ANONPORT;
  1002. }
  1003. } else {
  1004. faddr = sin->sin_addr;
  1005. fport = sin->sin_port;
  1006. }
  1007. } else {
  1008. INP_LOCK_ASSERT(inp);
  1009. faddr = inp->inp_faddr;
  1010. fport = inp->inp_fport;
  1011. if (faddr.s_addr == INADDR_ANY) {
  1012. error = ENOTCONN;
  1013. goto release;
  1014. }
  1015. }
  1016. /*
  1017. * Calculate data length and get a mbuf for UDP, IP, and possible
  1018. * link-layer headers. Immediate slide the data pointer back forward
  1019. * since we won't use that space at this layer.
  1020. */
  1021. M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_DONTWAIT);
  1022. if (m == NULL) {
  1023. error = ENOBUFS;
  1024. goto release;
  1025. }
  1026. m->m_hdr.mh_data += max_linkhdr;
  1027. m->m_hdr.mh_len -= max_linkhdr;
  1028. m->M_dat.MH.MH_pkthdr.len -= max_linkhdr;
  1029. /*
  1030. * Fill in mbuf with extended UDP header and addresses and length put
  1031. * into network format.
  1032. */
  1033. ui = mtod(m, struct udpiphdr *);
  1034. bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */
  1035. ui->ui_pr = IPPROTO_UDP;
  1036. ui->ui_src = laddr;
  1037. ui->ui_dst = faddr;
  1038. ui->ui_sport = lport;
  1039. ui->ui_dport = fport;
  1040. ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
  1041. /*
  1042. * Set the Don't Fragment bit in the IP header.
  1043. */
  1044. if (inp->inp_flags & INP_DONTFRAG) {
  1045. struct ip *ip;
  1046. ip = (struct ip *)&ui->ui_i;
  1047. ip->ip_off |= IP_DF;
  1048. }
  1049. ipflags = 0;
  1050. if (inp->inp_socket->so_options & SO_DONTROUTE)
  1051. ipflags |= IP_ROUTETOIF;
  1052. if (inp->inp_socket->so_options & SO_BROADCAST)
  1053. ipflags |= IP_ALLOWBROADCAST;
  1054. if (inp->inp_flags & INP_ONESBCAST)
  1055. ipflags |= IP_SENDONES;
  1056. #ifdef MAC
  1057. mac_inpcb_create_mbuf(inp, m);
  1058. #endif
  1059. /*
  1060. * Set up checksum and output datagram.
  1061. */
  1062. if (V_udp_cksum) {
  1063. if (inp->inp_flags & INP_ONESBCAST)
  1064. faddr.s_addr = INADDR_BROADCAST;
  1065. ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
  1066. htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
  1067. m->M_dat.MH.MH_pkthdr.csum_flags = CSUM_UDP;
  1068. m->M_dat.MH.MH_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
  1069. } else
  1070. ui->ui_sum = 0;
  1071. ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
  1072. ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */
  1073. ((struct ip *)ui)->ip_tos = tos; /* XXX */
  1074. UDPSTAT_INC(udps_opackets);
  1075. if (unlock_udbinfo == UH_WLOCKED)
  1076. INP_HASH_WUNLOCK(&V_udbinfo);
  1077. else if (unlock_udbinfo == UH_RLOCKED)
  1078. INP_HASH_RUNLOCK(&V_udbinfo);
  1079. error = ip_output(m, inp->inp_options, NULL, ipflags,
  1080. inp->inp_moptions, inp);
  1081. INP_UNLOCK(inp);
  1082. return (error);
  1083. release:
  1084. if (unlock_udbinfo == UH_WLOCKED) {
  1085. INP_HASH_WUNLOCK(&V_udbinfo);
  1086. INP_UNLOCK(inp);
  1087. } else if (unlock_udbinfo == UH_RLOCKED) {
  1088. INP_HASH_RUNLOCK(&V_udbinfo);
  1089. INP_UNLOCK(inp);
  1090. } else
  1091. INP_UNLOCK(inp);
  1092. m_freem(m);
  1093. return (error);
  1094. }
  1095. #if defined(IPSEC) && defined(IPSEC_NAT_T)
  1096. /*
  1097. * Potentially decap ESP in UDP frame. Check for an ESP header
  1098. * and optional marker; if present, strip the UDP header and
  1099. * push the result through IPSec.
  1100. *
  1101. * Returns mbuf to be processed (potentially re-allocated) or
  1102. * NULL if consumed and/or processed.
  1103. */
  1104. static struct mbuf *
  1105. udp4_espdecap(struct inpcb *inp, struct mbuf *m, int off)
  1106. {
  1107. size_t minlen, payload, skip, iphlen;
  1108. caddr_t data;
  1109. struct udpcb *up;
  1110. struct m_tag *tag;
  1111. struct udphdr *udphdr;
  1112. struct ip *ip;
  1113. INP_LOCK_ASSERT(inp);
  1114. /*
  1115. * Pull up data so the longest case is contiguous:
  1116. * IP/UDP hdr + non ESP marker + ESP hdr.
  1117. */
  1118. minlen = off + sizeof(uint64_t) + sizeof(struct esp);
  1119. if (minlen > m->M_dat.MH.MH_pkthdr.len)
  1120. minlen = m->M_dat.MH.MH_pkthdr.len;
  1121. if ((m = m_pullup(m, minlen)) == NULL) {
  1122. V_ipsec4stat.in_inval++;
  1123. return (NULL); /* Bypass caller processing. */
  1124. }
  1125. data = mtod(m, caddr_t); /* Points to ip header. */
  1126. payload = m->m_hdr.mh_len - off; /* Size of payload. */
  1127. if (payload == 1 && data[off] == '\xff')
  1128. return (m); /* NB: keepalive packet, no decap. */
  1129. up = intoudpcb(inp);
  1130. KASSERT(up != NULL, ("%s: udpcb NULL", __func__));
  1131. KASSERT((up->u_flags & UF_ESPINUDP_ALL) != 0,
  1132. ("u_flags 0x%x", up->u_flags));
  1133. /*
  1134. * Check that the payload is large enough to hold an
  1135. * ESP header and compute the amount of data to remove.
  1136. *
  1137. * NB: the caller has already done a pullup for us.
  1138. * XXX can we assume alignment and eliminate bcopys?
  1139. */
  1140. if (up->u_flags & UF_ESPINUDP_NON_IKE) {
  1141. /*
  1142. * draft-ietf-ipsec-nat-t-ike-0[01].txt and
  1143. * draft-ietf-ipsec-udp-encaps-(00/)01.txt, ignoring
  1144. * possible AH mode non-IKE marker+non-ESP marker
  1145. * from draft-ietf-ipsec-udp-encaps-00.txt.
  1146. */
  1147. uint64_t marker;
  1148. if (payload <= sizeof(uint64_t) + sizeof(struct esp))
  1149. return (m); /* NB: no decap. */
  1150. bcopy(data + off, &marker, sizeof(uint64_t));
  1151. if (marker != 0) /* Non-IKE marker. */
  1152. return (m); /* NB: no decap. */
  1153. skip = sizeof(uint64_t) + sizeof(struct udphdr);
  1154. } else {
  1155. uint32_t spi;
  1156. if (payload <= sizeof(struct esp)) {
  1157. V_ipsec4stat.in_inval++;
  1158. m_freem(m);
  1159. return (NULL); /* Discard. */
  1160. }
  1161. bcopy(data + off, &spi, sizeof(uint32_t));
  1162. if (spi == 0) /* Non-ESP marker. */
  1163. return (m); /* NB: no decap. */
  1164. skip = sizeof(struct udphdr);
  1165. }
  1166. /*
  1167. * Setup a PACKET_TAG_IPSEC_NAT_T_PORT tag to remember
  1168. * the UDP ports. This is required if we want to select
  1169. * the right SPD for multiple hosts behind same NAT.
  1170. *
  1171. * NB: ports are maintained in network byte order everywhere
  1172. * in the NAT-T code.
  1173. */
  1174. tag = m_tag_get(PACKET_TAG_IPSEC_NAT_T_PORTS,
  1175. 2 * sizeof(uint16_t), M_NOWAIT);
  1176. if (tag == NULL) {
  1177. V_ipsec4stat.in_nomem++;
  1178. m_freem(m);
  1179. return (NULL); /* Discard. */
  1180. }
  1181. iphlen = off - sizeof(struct udphdr);
  1182. udphdr = (struct udphdr *)(data + iphlen);
  1183. ((uint16_t *)(tag + 1))[0] = udphdr->uh_sport;
  1184. ((uint16_t *)(tag + 1))[1] = udphdr->uh_dport;
  1185. m_tag_prepend(m, tag);
  1186. /*
  1187. * Remove the UDP header (and possibly the non ESP marker)
  1188. * IP header length is iphlen
  1189. * Before:
  1190. * <--- off --->
  1191. * +----+------+-----+
  1192. * | IP | UDP | ESP |
  1193. * +----+------+-----+
  1194. * <-skip->
  1195. * After:
  1196. * +----+-----+
  1197. * | IP | ESP |
  1198. * +----+-----+
  1199. * <-skip->
  1200. */
  1201. ovbcopy(data, data + skip, iphlen);
  1202. m_adj(m, skip);
  1203. ip = mtod(m, struct ip *);
  1204. ip->ip_len -= skip;
  1205. ip->ip_p = IPPROTO_ESP;
  1206. /*
  1207. * We cannot yet update the cksums so clear any
  1208. * h/w cksum flags as they are no longer valid.
  1209. */
  1210. if (m->M_dat.MH.MH_pkthdr.csum_flags & CSUM_DATA_VALID)
  1211. m->M_dat.MH.MH_pkthdr.csum_flags &= ~(CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
  1212. (void) ipsec4_common_input(m, iphlen, ip->ip_p);
  1213. return (NULL); /* NB: consumed, bypass processing. */
  1214. }
  1215. #endif /* defined(IPSEC) && defined(IPSEC_NAT_T) */
  1216. static void
  1217. udp_abort(struct socket *so)
  1218. {
  1219. struct inpcb *inp;
  1220. inp = sotoinpcb(so);
  1221. KASSERT(inp != NULL, ("udp_abort: inp == NULL"));
  1222. INP_LOCK(inp);
  1223. if (inp->inp_faddr.s_addr != INADDR_ANY) {
  1224. INP_HASH_WLOCK(&V_udbinfo);
  1225. in_pcbdisconnect(inp);
  1226. inp->inp_laddr.s_addr = INADDR_ANY;
  1227. INP_HASH_WUNLOCK(&V_udbinfo);
  1228. soisdisconnected(so);
  1229. }
  1230. INP_UNLOCK(inp);
  1231. }
  1232. static int
  1233. udp_attach(struct socket *so, int proto, struct thread *td)
  1234. {
  1235. struct inpcb *inp;
  1236. int error;
  1237. inp = sotoinpcb(so);
  1238. KASSERT(inp == NULL, ("udp_attach: inp != NULL"));
  1239. error = soreserve_internal(so, udp_sendspace, udp_recvspace);
  1240. if (error)
  1241. return (error);
  1242. INP_INFO_WLOCK(&V_udbinfo);
  1243. inp = new inpcb(so, &V_udbinfo);
  1244. inp->inp_vflag |= INP_IPV4;
  1245. inp->inp_ip_ttl = V_ip_defttl;
  1246. error = udp_newudpcb(inp);
  1247. if (error) {
  1248. in_pcbdetach(inp);
  1249. in_pcbfree(inp);
  1250. INP_INFO_WUNLOCK(&V_udbinfo);
  1251. return (error);
  1252. }
  1253. INP_UNLOCK(inp);
  1254. INP_INFO_WUNLOCK(&V_udbinfo);
  1255. return (0);
  1256. }
  1257. #endif /* INET */
  1258. int
  1259. udp_set_kernel_tunneling(struct socket *so, udp_tun_func_t f)
  1260. {
  1261. struct inpcb *inp;
  1262. struct udpcb *up;
  1263. KASSERT(so->so_type == SOCK_DGRAM,
  1264. ("udp_set_kernel_tunneling: !dgram"));
  1265. inp = sotoinpcb(so);
  1266. KASSERT(inp != NULL, ("udp_set_kernel_tunneling: inp == NULL"));
  1267. INP_LOCK(inp);
  1268. up = intoudpcb(inp);
  1269. if (up->u_tun_func != NULL) {
  1270. INP_UNLOCK(inp);
  1271. return (EBUSY);
  1272. }
  1273. up->u_tun_func = f;
  1274. INP_UNLOCK(inp);
  1275. return (0);
  1276. }
  1277. #ifdef INET
  1278. static int
  1279. udp_bind(struct socket *so, struct bsd_sockaddr *nam, struct thread *td)
  1280. {
  1281. struct inpcb *inp;
  1282. int error;
  1283. inp = sotoinpcb(so);
  1284. KASSERT(inp != NULL, ("udp_bind: inp == NULL"));
  1285. INP_LOCK(inp);
  1286. INP_HASH_WLOCK(&V_udbinfo);
  1287. error = in_pcbbind(inp, nam, 0);
  1288. INP_HASH_WUNLOCK(&V_udbinfo);
  1289. INP_UNLOCK(inp);
  1290. return (error);
  1291. }
  1292. static void
  1293. udp_close(struct socket *so)
  1294. {
  1295. struct inpcb *inp;
  1296. inp = sotoinpcb(so);
  1297. KASSERT(inp != NULL, ("udp_close: inp == NULL"));
  1298. INP_LOCK(inp);
  1299. if (inp->inp_faddr.s_addr != INADDR_ANY) {
  1300. INP_HASH_WLOCK(&V_udbinfo);
  1301. in_pcbdisconnect(inp);
  1302. inp->inp_laddr.s_addr = INADDR_ANY;
  1303. INP_HASH_WUNLOCK(&V_udbinfo);
  1304. soisdisconnected(so);
  1305. }
  1306. INP_UNLOCK(inp);
  1307. }
  1308. static int
  1309. udp_connect(struct socket *so, struct bsd_sockaddr *nam, struct thread *td)
  1310. {
  1311. struct inpcb *inp;
  1312. int error;
  1313. struct bsd_sockaddr_in *sin;
  1314. inp = sotoinpcb(so);
  1315. KASSERT(inp != NULL, ("udp_connect: inp == NULL"));
  1316. INP_LOCK(inp);
  1317. if (inp->inp_faddr.s_addr != INADDR_ANY) {
  1318. INP_UNLOCK(inp);
  1319. return (EISCONN);
  1320. }
  1321. sin = (struct bsd_sockaddr_in *)nam;
  1322. INP_HASH_WLOCK(&V_udbinfo);
  1323. error = in_pcbconnect(inp, nam, 0);
  1324. INP_HASH_WUNLOCK(&V_udbinfo);
  1325. if (error == 0)
  1326. soisconnected(so);
  1327. INP_UNLOCK(inp);
  1328. return (error);
  1329. }
  1330. static void
  1331. udp_detach(struct socket *so)
  1332. {
  1333. struct inpcb *inp;
  1334. struct udpcb *up;
  1335. inp = sotoinpcb(so);
  1336. KASSERT(inp != NULL, ("udp_detach: inp == NULL"));
  1337. KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
  1338. ("udp_detach: not disconnected"));
  1339. INP_INFO_WLOCK(&V_udbinfo);
  1340. INP_LOCK(inp);
  1341. up = intoudpcb(inp);
  1342. KASSERT(up != NULL, ("%s: up == NULL", __func__));
  1343. inp->inp_ppcb = NULL;
  1344. in_pcbdetach(inp);
  1345. in_pcbfree(inp);
  1346. INP_INFO_WUNLOCK(&V_udbinfo);
  1347. udp_discardcb(up);
  1348. }
  1349. static int
  1350. udp_disconnect(struct socket *so)
  1351. {
  1352. struct inpcb *inp;
  1353. inp = sotoinpcb(so);
  1354. KASSERT(inp != NULL, ("udp_disconnect: inp == NULL"));
  1355. INP_LOCK(inp);
  1356. if (inp->inp_faddr.s_addr == INADDR_ANY) {
  1357. INP_UNLOCK(inp);
  1358. return (ENOTCONN);
  1359. }
  1360. INP_HASH_WLOCK(&V_udbinfo);
  1361. in_pcbdisconnect(inp);
  1362. inp->inp_laddr.s_addr = INADDR_ANY;
  1363. INP_HASH_WUNLOCK(&V_udbinfo);
  1364. SOCK_LOCK(so);
  1365. so->so_state &= ~SS_ISCONNECTED; /* XXX */
  1366. SOCK_UNLOCK(so);
  1367. INP_UNLOCK(inp);
  1368. return (0);
  1369. }
  1370. static int
  1371. udp_send(struct socket *so, int flags, struct mbuf *m, struct bsd_sockaddr *addr,
  1372. struct mbuf *control, struct thread *td)
  1373. {
  1374. struct inpcb *inp;
  1375. inp = sotoinpcb(so);
  1376. KASSERT(inp != NULL, ("udp_send: inp == NULL"));
  1377. return (udp_output(inp, m, addr, control, td));
  1378. }
  1379. #endif /* INET */
  1380. int
  1381. udp_shutdown(struct socket *so)
  1382. {
  1383. struct inpcb *inp;
  1384. inp = sotoinpcb(so);
  1385. KASSERT(inp != NULL, ("udp_shutdown: inp == NULL"));
  1386. INP_LOCK(inp);
  1387. socantsendmore(so);
  1388. INP_UNLOCK(inp);
  1389. return (0);
  1390. }
  1391. #ifdef INET
  1392. struct pr_usrreqs udp_usrreqs = initialize_with([] (pr_usrreqs& x) {
  1393. x.pru_abort = udp_abort;
  1394. x.pru_attach = udp_attach;
  1395. x.pru_bind = udp_bind;
  1396. x.pru_connect = udp_connect;
  1397. x.pru_control = in_control;
  1398. x.pru_detach = udp_detach;
  1399. x.pru_disconnect = udp_disconnect;
  1400. x.pru_peeraddr = in_getpeeraddr;
  1401. x.pru_send = udp_send;
  1402. x.pru_soreceive = soreceive_dgram;
  1403. x.pru_sosend = sosend_dgram;
  1404. x.pru_shutdown = udp_shutdown;
  1405. x.pru_sockaddr = in_getsockaddr;
  1406. x.pru_sosetlabel = in_pcbsosetlabel;
  1407. x.pru_close = udp_close;
  1408. });
  1409. #endif /* INET */