udp6_usrreq.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
  5. * Copyright (c) 2010-2011 Juniper Networks, Inc.
  6. * Copyright (c) 2014 Kevin Lo
  7. * All rights reserved.
  8. *
  9. * Portions of this software were developed by Robert N. M. Watson under
  10. * contract to Juniper Networks, Inc.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * 3. Neither the name of the project nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. *
  36. * $KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $
  37. * $KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei Exp $
  38. */
  39. /*-
  40. * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
  41. * The Regents of the University of California.
  42. * All rights reserved.
  43. *
  44. * Redistribution and use in source and binary forms, with or without
  45. * modification, are permitted provided that the following conditions
  46. * are met:
  47. * 1. Redistributions of source code must retain the above copyright
  48. * notice, this list of conditions and the following disclaimer.
  49. * 2. Redistributions in binary form must reproduce the above copyright
  50. * notice, this list of conditions and the following disclaimer in the
  51. * documentation and/or other materials provided with the distribution.
  52. * 3. Neither the name of the University nor the names of its contributors
  53. * may be used to endorse or promote products derived from this software
  54. * without specific prior written permission.
  55. *
  56. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  57. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  58. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  59. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  60. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  61. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  62. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  63. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  64. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  65. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  66. * SUCH DAMAGE.
  67. *
  68. * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
  69. */
  70. #include <sys/cdefs.h>
  71. __FBSDID("$FreeBSD$");
  72. #include "opt_inet.h"
  73. #include "opt_inet6.h"
  74. #include "opt_ipsec.h"
  75. #include "opt_route.h"
  76. #include "opt_rss.h"
  77. #include <sys/param.h>
  78. #include <sys/jail.h>
  79. #include <sys/kernel.h>
  80. #include <sys/lock.h>
  81. #include <sys/mbuf.h>
  82. #include <sys/priv.h>
  83. #include <sys/proc.h>
  84. #include <sys/protosw.h>
  85. #include <sys/sdt.h>
  86. #include <sys/signalvar.h>
  87. #include <sys/socket.h>
  88. #include <sys/socketvar.h>
  89. #include <sys/sx.h>
  90. #include <sys/sysctl.h>
  91. #include <sys/syslog.h>
  92. #include <sys/systm.h>
  93. #include <net/if.h>
  94. #include <net/if_var.h>
  95. #include <net/if_types.h>
  96. #include <net/route.h>
  97. #include <net/rss_config.h>
  98. #include <netinet/in.h>
  99. #include <netinet/in_kdtrace.h>
  100. #include <netinet/in_pcb.h>
  101. #include <netinet/in_systm.h>
  102. #include <netinet/in_var.h>
  103. #include <netinet/ip.h>
  104. #include <netinet/ip6.h>
  105. #include <netinet/icmp6.h>
  106. #include <netinet/ip_var.h>
  107. #include <netinet/udp.h>
  108. #include <netinet/udp_var.h>
  109. #include <netinet/udplite.h>
  110. #include <netinet6/ip6protosw.h>
  111. #include <netinet6/ip6_var.h>
  112. #include <netinet6/in6_fib.h>
  113. #include <netinet6/in6_pcb.h>
  114. #include <netinet6/in6_rss.h>
  115. #include <netinet6/udp6_var.h>
  116. #include <netinet6/scope6_var.h>
  117. #include <netipsec/ipsec_support.h>
  118. #include <security/mac/mac_framework.h>
  119. VNET_DEFINE(int, zero_checksum_port) = 0;
  120. #define V_zero_checksum_port VNET(zero_checksum_port)
  121. SYSCTL_INT(_net_inet6_udp6, OID_AUTO, rfc6935_port, CTLFLAG_VNET | CTLFLAG_RW,
  122. &VNET_NAME(zero_checksum_port), 0,
  123. "Zero UDP checksum allowed for traffic to/from this port.");
  124. /*
  125. * UDP protocol implementation.
  126. * Per RFC 768, August, 1980.
  127. */
  128. extern struct protosw inetsw[];
  129. static void udp6_detach(struct socket *so);
  130. static int
  131. udp6_append(struct inpcb *inp, struct mbuf *n, int off,
  132. struct sockaddr_in6 *fromsa)
  133. {
  134. struct socket *so;
  135. struct mbuf *opts = NULL, *tmp_opts;
  136. struct udpcb *up;
  137. INP_LOCK_ASSERT(inp);
  138. /*
  139. * Engage the tunneling protocol.
  140. */
  141. up = intoudpcb(inp);
  142. if (up->u_tun_func != NULL) {
  143. in_pcbref(inp);
  144. INP_RUNLOCK(inp);
  145. (*up->u_tun_func)(n, off, inp, (struct sockaddr *)&fromsa[0],
  146. up->u_tun_ctx);
  147. INP_RLOCK(inp);
  148. return (in_pcbrele_rlocked(inp));
  149. }
  150. #if defined(IPSEC) || defined(IPSEC_SUPPORT)
  151. /* Check AH/ESP integrity. */
  152. if (IPSEC_ENABLED(ipv6)) {
  153. if (IPSEC_CHECK_POLICY(ipv6, n, inp) != 0) {
  154. m_freem(n);
  155. return (0);
  156. }
  157. }
  158. #endif /* IPSEC */
  159. #ifdef MAC
  160. if (mac_inpcb_check_deliver(inp, n) != 0) {
  161. m_freem(n);
  162. return (0);
  163. }
  164. #endif
  165. opts = NULL;
  166. if (inp->inp_flags & INP_CONTROLOPTS ||
  167. inp->inp_socket->so_options & SO_TIMESTAMP)
  168. ip6_savecontrol(inp, n, &opts);
  169. if ((inp->inp_vflag & INP_IPV6) && (inp->inp_flags2 & INP_ORIGDSTADDR)) {
  170. tmp_opts = sbcreatecontrol((caddr_t)&fromsa[1],
  171. sizeof(struct sockaddr_in6), IPV6_ORIGDSTADDR, IPPROTO_IPV6);
  172. if (tmp_opts) {
  173. if (opts) {
  174. tmp_opts->m_next = opts;
  175. opts = tmp_opts;
  176. } else
  177. opts = tmp_opts;
  178. }
  179. }
  180. m_adj(n, off + sizeof(struct udphdr));
  181. so = inp->inp_socket;
  182. SOCKBUF_LOCK(&so->so_rcv);
  183. if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)&fromsa[0], n,
  184. opts) == 0) {
  185. SOCKBUF_UNLOCK(&so->so_rcv);
  186. m_freem(n);
  187. if (opts)
  188. m_freem(opts);
  189. UDPSTAT_INC(udps_fullsock);
  190. } else
  191. sorwakeup_locked(so);
  192. return (0);
  193. }
  194. int
  195. udp6_input(struct mbuf **mp, int *offp, int proto)
  196. {
  197. struct mbuf *m = *mp;
  198. struct ifnet *ifp;
  199. struct ip6_hdr *ip6;
  200. struct udphdr *uh;
  201. struct inpcb *inp;
  202. struct inpcbinfo *pcbinfo;
  203. struct udpcb *up;
  204. int off = *offp;
  205. int cscov_partial;
  206. int plen, ulen;
  207. struct sockaddr_in6 fromsa[2];
  208. struct m_tag *fwd_tag;
  209. uint16_t uh_sum;
  210. uint8_t nxt;
  211. NET_EPOCH_ASSERT();
  212. ifp = m->m_pkthdr.rcvif;
  213. if (m->m_len < off + sizeof(struct udphdr)) {
  214. m = m_pullup(m, off + sizeof(struct udphdr));
  215. if (m == NULL) {
  216. IP6STAT_INC(ip6s_exthdrtoolong);
  217. *mp = NULL;
  218. return (IPPROTO_DONE);
  219. }
  220. }
  221. ip6 = mtod(m, struct ip6_hdr *);
  222. uh = (struct udphdr *)((caddr_t)ip6 + off);
  223. UDPSTAT_INC(udps_ipackets);
  224. /*
  225. * Destination port of 0 is illegal, based on RFC768.
  226. */
  227. if (uh->uh_dport == 0)
  228. goto badunlocked;
  229. plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
  230. ulen = ntohs((u_short)uh->uh_ulen);
  231. nxt = proto;
  232. cscov_partial = (nxt == IPPROTO_UDPLITE) ? 1 : 0;
  233. if (nxt == IPPROTO_UDPLITE) {
  234. /* Zero means checksum over the complete packet. */
  235. if (ulen == 0)
  236. ulen = plen;
  237. if (ulen == plen)
  238. cscov_partial = 0;
  239. if ((ulen < sizeof(struct udphdr)) || (ulen > plen)) {
  240. /* XXX: What is the right UDPLite MIB counter? */
  241. goto badunlocked;
  242. }
  243. if (uh->uh_sum == 0) {
  244. /* XXX: What is the right UDPLite MIB counter? */
  245. goto badunlocked;
  246. }
  247. } else {
  248. if ((ulen < sizeof(struct udphdr)) || (plen != ulen)) {
  249. UDPSTAT_INC(udps_badlen);
  250. goto badunlocked;
  251. }
  252. if (uh->uh_sum == 0) {
  253. UDPSTAT_INC(udps_nosum);
  254. /*
  255. * dport 0 was rejected earlier so this is OK even if
  256. * zero_checksum_port is 0 (which is its default value).
  257. */
  258. if (ntohs(uh->uh_dport) == V_zero_checksum_port)
  259. goto skip_checksum;
  260. else
  261. goto badunlocked;
  262. }
  263. }
  264. if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) &&
  265. !cscov_partial) {
  266. if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
  267. uh_sum = m->m_pkthdr.csum_data;
  268. else
  269. uh_sum = in6_cksum_pseudo(ip6, ulen, nxt,
  270. m->m_pkthdr.csum_data);
  271. uh_sum ^= 0xffff;
  272. } else
  273. uh_sum = in6_cksum_partial(m, nxt, off, plen, ulen);
  274. if (uh_sum != 0) {
  275. UDPSTAT_INC(udps_badsum);
  276. goto badunlocked;
  277. }
  278. skip_checksum:
  279. /*
  280. * Construct sockaddr format source address.
  281. */
  282. init_sin6(&fromsa[0], m, 0);
  283. fromsa[0].sin6_port = uh->uh_sport;
  284. init_sin6(&fromsa[1], m, 1);
  285. fromsa[1].sin6_port = uh->uh_dport;
  286. pcbinfo = udp_get_inpcbinfo(nxt);
  287. if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
  288. struct inpcb *last;
  289. struct inpcbhead *pcblist;
  290. struct ip6_moptions *imo;
  291. /*
  292. * In the event that laddr should be set to the link-local
  293. * address (this happens in RIPng), the multicast address
  294. * specified in the received packet will not match laddr. To
  295. * handle this situation, matching is relaxed if the
  296. * receiving interface is the same as one specified in the
  297. * socket and if the destination multicast address matches
  298. * one of the multicast groups specified in the socket.
  299. */
  300. /*
  301. * KAME note: traditionally we dropped udpiphdr from mbuf
  302. * here. We need udphdr for IPsec processing so we do that
  303. * later.
  304. */
  305. pcblist = udp_get_pcblist(nxt);
  306. last = NULL;
  307. CK_LIST_FOREACH(inp, pcblist, inp_list) {
  308. if ((inp->inp_vflag & INP_IPV6) == 0)
  309. continue;
  310. if (inp->inp_lport != uh->uh_dport)
  311. continue;
  312. if (inp->inp_fport != 0 &&
  313. inp->inp_fport != uh->uh_sport)
  314. continue;
  315. if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
  316. if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
  317. &ip6->ip6_dst))
  318. continue;
  319. }
  320. if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
  321. if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
  322. &ip6->ip6_src) ||
  323. inp->inp_fport != uh->uh_sport)
  324. continue;
  325. }
  326. /*
  327. * XXXRW: Because we weren't holding either the inpcb
  328. * or the hash lock when we checked for a match
  329. * before, we should probably recheck now that the
  330. * inpcb lock is (supposed to be) held.
  331. */
  332. /*
  333. * Handle socket delivery policy for any-source
  334. * and source-specific multicast. [RFC3678]
  335. */
  336. imo = inp->in6p_moptions;
  337. if (imo && IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
  338. struct sockaddr_in6 mcaddr;
  339. int blocked;
  340. INP_RLOCK(inp);
  341. if (__predict_false(inp->inp_flags2 & INP_FREED)) {
  342. INP_RUNLOCK(inp);
  343. continue;
  344. }
  345. bzero(&mcaddr, sizeof(struct sockaddr_in6));
  346. mcaddr.sin6_len = sizeof(struct sockaddr_in6);
  347. mcaddr.sin6_family = AF_INET6;
  348. mcaddr.sin6_addr = ip6->ip6_dst;
  349. blocked = im6o_mc_filter(imo, ifp,
  350. (struct sockaddr *)&mcaddr,
  351. (struct sockaddr *)&fromsa[0]);
  352. if (blocked != MCAST_PASS) {
  353. if (blocked == MCAST_NOTGMEMBER)
  354. IP6STAT_INC(ip6s_notmember);
  355. if (blocked == MCAST_NOTSMEMBER ||
  356. blocked == MCAST_MUTED)
  357. UDPSTAT_INC(udps_filtermcast);
  358. INP_RUNLOCK(inp); /* XXX */
  359. continue;
  360. }
  361. INP_RUNLOCK(inp);
  362. }
  363. if (last != NULL) {
  364. struct mbuf *n;
  365. if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) !=
  366. NULL) {
  367. INP_RLOCK(last);
  368. if (__predict_true(last->inp_flags2 & INP_FREED) == 0) {
  369. if (nxt == IPPROTO_UDPLITE)
  370. UDPLITE_PROBE(receive, NULL, last,
  371. ip6, last, uh);
  372. else
  373. UDP_PROBE(receive, NULL, last,
  374. ip6, last, uh);
  375. if (udp6_append(last, n, off, fromsa)) {
  376. /* XXX-BZ do we leak m here? */
  377. *mp = NULL;
  378. return (IPPROTO_DONE);
  379. }
  380. }
  381. INP_RUNLOCK(last);
  382. }
  383. }
  384. last = inp;
  385. /*
  386. * Don't look for additional matches if this one does
  387. * not have either the SO_REUSEPORT or SO_REUSEADDR
  388. * socket options set. This heuristic avoids
  389. * searching through all pcbs in the common case of a
  390. * non-shared port. It assumes that an application
  391. * will never clear these options after setting them.
  392. */
  393. if ((last->inp_socket->so_options &
  394. (SO_REUSEPORT|SO_REUSEPORT_LB|SO_REUSEADDR)) == 0)
  395. break;
  396. }
  397. if (last == NULL) {
  398. /*
  399. * No matching pcb found; discard datagram. (No need
  400. * to send an ICMP Port Unreachable for a broadcast
  401. * or multicast datgram.)
  402. */
  403. UDPSTAT_INC(udps_noport);
  404. UDPSTAT_INC(udps_noportmcast);
  405. goto badunlocked;
  406. }
  407. INP_RLOCK(last);
  408. if (__predict_true(last->inp_flags2 & INP_FREED) == 0) {
  409. if (nxt == IPPROTO_UDPLITE)
  410. UDPLITE_PROBE(receive, NULL, last, ip6, last, uh);
  411. else
  412. UDP_PROBE(receive, NULL, last, ip6, last, uh);
  413. if (udp6_append(last, m, off, fromsa) == 0)
  414. INP_RUNLOCK(last);
  415. } else
  416. INP_RUNLOCK(last);
  417. *mp = NULL;
  418. return (IPPROTO_DONE);
  419. }
  420. /*
  421. * Locate pcb for datagram.
  422. */
  423. /*
  424. * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
  425. */
  426. if ((m->m_flags & M_IP6_NEXTHOP) &&
  427. (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
  428. struct sockaddr_in6 *next_hop6;
  429. next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
  430. /*
  431. * Transparently forwarded. Pretend to be the destination.
  432. * Already got one like this?
  433. */
  434. inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
  435. uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
  436. INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif, m);
  437. if (!inp) {
  438. /*
  439. * It's new. Try to find the ambushing socket.
  440. * Because we've rewritten the destination address,
  441. * any hardware-generated hash is ignored.
  442. */
  443. inp = in6_pcblookup(pcbinfo, &ip6->ip6_src,
  444. uh->uh_sport, &next_hop6->sin6_addr,
  445. next_hop6->sin6_port ? htons(next_hop6->sin6_port) :
  446. uh->uh_dport, INPLOOKUP_WILDCARD |
  447. INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif);
  448. }
  449. /* Remove the tag from the packet. We don't need it anymore. */
  450. m_tag_delete(m, fwd_tag);
  451. m->m_flags &= ~M_IP6_NEXTHOP;
  452. } else
  453. inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
  454. uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
  455. INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB,
  456. m->m_pkthdr.rcvif, m);
  457. if (inp == NULL) {
  458. if (V_udp_log_in_vain) {
  459. char ip6bufs[INET6_ADDRSTRLEN];
  460. char ip6bufd[INET6_ADDRSTRLEN];
  461. log(LOG_INFO,
  462. "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
  463. ip6_sprintf(ip6bufd, &ip6->ip6_dst),
  464. ntohs(uh->uh_dport),
  465. ip6_sprintf(ip6bufs, &ip6->ip6_src),
  466. ntohs(uh->uh_sport));
  467. }
  468. if (nxt == IPPROTO_UDPLITE)
  469. UDPLITE_PROBE(receive, NULL, NULL, ip6, NULL, uh);
  470. else
  471. UDP_PROBE(receive, NULL, NULL, ip6, NULL, uh);
  472. UDPSTAT_INC(udps_noport);
  473. if (m->m_flags & M_MCAST) {
  474. printf("UDP6: M_MCAST is set in a unicast packet.\n");
  475. UDPSTAT_INC(udps_noportmcast);
  476. goto badunlocked;
  477. }
  478. if (V_udp_blackhole)
  479. goto badunlocked;
  480. icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
  481. *mp = NULL;
  482. return (IPPROTO_DONE);
  483. }
  484. INP_RLOCK_ASSERT(inp);
  485. up = intoudpcb(inp);
  486. if (cscov_partial) {
  487. if (up->u_rxcslen == 0 || up->u_rxcslen > ulen) {
  488. INP_RUNLOCK(inp);
  489. m_freem(m);
  490. *mp = NULL;
  491. return (IPPROTO_DONE);
  492. }
  493. }
  494. if (nxt == IPPROTO_UDPLITE)
  495. UDPLITE_PROBE(receive, NULL, inp, ip6, inp, uh);
  496. else
  497. UDP_PROBE(receive, NULL, inp, ip6, inp, uh);
  498. if (udp6_append(inp, m, off, fromsa) == 0)
  499. INP_RUNLOCK(inp);
  500. *mp = NULL;
  501. return (IPPROTO_DONE);
  502. badunlocked:
  503. m_freem(m);
  504. *mp = NULL;
  505. return (IPPROTO_DONE);
  506. }
  507. static void
  508. udp6_common_ctlinput(int cmd, struct sockaddr *sa, void *d,
  509. struct inpcbinfo *pcbinfo)
  510. {
  511. struct udphdr uh;
  512. struct ip6_hdr *ip6;
  513. struct mbuf *m;
  514. int off = 0;
  515. struct ip6ctlparam *ip6cp = NULL;
  516. const struct sockaddr_in6 *sa6_src = NULL;
  517. void *cmdarg;
  518. struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
  519. struct udp_portonly {
  520. u_int16_t uh_sport;
  521. u_int16_t uh_dport;
  522. } *uhp;
  523. if (sa->sa_family != AF_INET6 ||
  524. sa->sa_len != sizeof(struct sockaddr_in6))
  525. return;
  526. if ((unsigned)cmd >= PRC_NCMDS)
  527. return;
  528. if (PRC_IS_REDIRECT(cmd))
  529. notify = in6_rtchange, d = NULL;
  530. else if (cmd == PRC_HOSTDEAD)
  531. d = NULL;
  532. else if (inet6ctlerrmap[cmd] == 0)
  533. return;
  534. /* if the parameter is from icmp6, decode it. */
  535. if (d != NULL) {
  536. ip6cp = (struct ip6ctlparam *)d;
  537. m = ip6cp->ip6c_m;
  538. ip6 = ip6cp->ip6c_ip6;
  539. off = ip6cp->ip6c_off;
  540. cmdarg = ip6cp->ip6c_cmdarg;
  541. sa6_src = ip6cp->ip6c_src;
  542. } else {
  543. m = NULL;
  544. ip6 = NULL;
  545. cmdarg = NULL;
  546. sa6_src = &sa6_any;
  547. }
  548. if (ip6) {
  549. /*
  550. * XXX: We assume that when IPV6 is non NULL,
  551. * M and OFF are valid.
  552. */
  553. /* Check if we can safely examine src and dst ports. */
  554. if (m->m_pkthdr.len < off + sizeof(*uhp))
  555. return;
  556. bzero(&uh, sizeof(uh));
  557. m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
  558. if (!PRC_IS_REDIRECT(cmd)) {
  559. /* Check to see if its tunneled */
  560. struct inpcb *inp;
  561. inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_dst,
  562. uh.uh_dport, &ip6->ip6_src, uh.uh_sport,
  563. INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB,
  564. m->m_pkthdr.rcvif, m);
  565. if (inp != NULL) {
  566. struct udpcb *up;
  567. up = intoudpcb(inp);
  568. if (up->u_icmp_func) {
  569. /* Yes it is. */
  570. INP_RUNLOCK(inp);
  571. (*up->u_icmp_func)(cmd, (struct sockaddr *)ip6cp->ip6c_src,
  572. d, up->u_tun_ctx);
  573. return;
  574. } else {
  575. /* Can't find it. */
  576. INP_RUNLOCK(inp);
  577. }
  578. }
  579. }
  580. (void)in6_pcbnotify(pcbinfo, sa, uh.uh_dport,
  581. (struct sockaddr *)ip6cp->ip6c_src, uh.uh_sport, cmd,
  582. cmdarg, notify);
  583. } else
  584. (void)in6_pcbnotify(pcbinfo, sa, 0,
  585. (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
  586. }
  587. void
  588. udp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
  589. {
  590. return (udp6_common_ctlinput(cmd, sa, d, &V_udbinfo));
  591. }
  592. void
  593. udplite6_ctlinput(int cmd, struct sockaddr *sa, void *d)
  594. {
  595. return (udp6_common_ctlinput(cmd, sa, d, &V_ulitecbinfo));
  596. }
  597. static int
  598. udp6_getcred(SYSCTL_HANDLER_ARGS)
  599. {
  600. struct xucred xuc;
  601. struct sockaddr_in6 addrs[2];
  602. struct epoch_tracker et;
  603. struct inpcb *inp;
  604. int error;
  605. error = priv_check(req->td, PRIV_NETINET_GETCRED);
  606. if (error)
  607. return (error);
  608. if (req->newlen != sizeof(addrs))
  609. return (EINVAL);
  610. if (req->oldlen != sizeof(struct xucred))
  611. return (EINVAL);
  612. error = SYSCTL_IN(req, addrs, sizeof(addrs));
  613. if (error)
  614. return (error);
  615. if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 ||
  616. (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) {
  617. return (error);
  618. }
  619. NET_EPOCH_ENTER(et);
  620. inp = in6_pcblookup(&V_udbinfo, &addrs[1].sin6_addr,
  621. addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port,
  622. INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
  623. NET_EPOCH_EXIT(et);
  624. if (inp != NULL) {
  625. INP_RLOCK_ASSERT(inp);
  626. if (inp->inp_socket == NULL)
  627. error = ENOENT;
  628. if (error == 0)
  629. error = cr_canseesocket(req->td->td_ucred,
  630. inp->inp_socket);
  631. if (error == 0)
  632. cru2x(inp->inp_cred, &xuc);
  633. INP_RUNLOCK(inp);
  634. } else
  635. error = ENOENT;
  636. if (error == 0)
  637. error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
  638. return (error);
  639. }
  640. SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred,
  641. CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_MPSAFE,
  642. 0, 0, udp6_getcred, "S,xucred",
  643. "Get the xucred of a UDP6 connection");
  644. static int
  645. udp6_output(struct socket *so, int flags_arg, struct mbuf *m,
  646. struct sockaddr *addr6, struct mbuf *control, struct thread *td)
  647. {
  648. struct inpcb *inp;
  649. struct ip6_hdr *ip6;
  650. struct udphdr *udp6;
  651. struct in6_addr *laddr, *faddr, in6a;
  652. struct ip6_pktopts *optp, opt;
  653. struct sockaddr_in6 *sin6, tmp;
  654. struct epoch_tracker et;
  655. int cscov_partial, error, flags, hlen, scope_ambiguous;
  656. u_int32_t ulen, plen;
  657. uint16_t cscov;
  658. u_short fport;
  659. uint8_t nxt;
  660. /* addr6 has been validated in udp6_send(). */
  661. sin6 = (struct sockaddr_in6 *)addr6;
  662. /*
  663. * In contrast to to IPv4 we do not validate the max. packet length
  664. * here due to IPv6 Jumbograms (RFC2675).
  665. */
  666. scope_ambiguous = 0;
  667. if (sin6) {
  668. /* Protect *addr6 from overwrites. */
  669. tmp = *sin6;
  670. sin6 = &tmp;
  671. /*
  672. * Application should provide a proper zone ID or the use of
  673. * default zone IDs should be enabled. Unfortunately, some
  674. * applications do not behave as it should, so we need a
  675. * workaround. Even if an appropriate ID is not determined,
  676. * we'll see if we can determine the outgoing interface. If we
  677. * can, determine the zone ID based on the interface below.
  678. */
  679. if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
  680. scope_ambiguous = 1;
  681. if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0) {
  682. if (control)
  683. m_freem(control);
  684. m_freem(m);
  685. return (error);
  686. }
  687. }
  688. inp = sotoinpcb(so);
  689. KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
  690. /*
  691. * In the following cases we want a write lock on the inp for either
  692. * local operations or for possible route cache updates in the IPv6
  693. * output path:
  694. * - on connected sockets (sin6 is NULL) for route cache updates,
  695. * - when we are not bound to an address and source port (it is
  696. * in6_pcbsetport() which will require the write lock).
  697. *
  698. * We check the inp fields before actually locking the inp, so
  699. * here exists a race, and we may WLOCK the inp and end with already
  700. * bound one by other thread. This is fine.
  701. */
  702. if (sin6 == NULL || (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
  703. inp->inp_lport == 0))
  704. INP_WLOCK(inp);
  705. else
  706. INP_RLOCK(inp);
  707. nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
  708. IPPROTO_UDP : IPPROTO_UDPLITE;
  709. #ifdef INET
  710. if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
  711. int hasv4addr;
  712. if (sin6 == NULL)
  713. hasv4addr = (inp->inp_vflag & INP_IPV4);
  714. else
  715. hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
  716. ? 1 : 0;
  717. if (hasv4addr) {
  718. struct pr_usrreqs *pru;
  719. /*
  720. * XXXRW: We release UDP-layer locks before calling
  721. * udp_send() in order to avoid recursion. However,
  722. * this does mean there is a short window where inp's
  723. * fields are unstable. Could this lead to a
  724. * potential race in which the factors causing us to
  725. * select the UDPv4 output routine are invalidated?
  726. */
  727. INP_UNLOCK(inp);
  728. if (sin6)
  729. in6_sin6_2_sin_in_sock((struct sockaddr *)sin6);
  730. pru = inetsw[ip_protox[nxt]].pr_usrreqs;
  731. /* addr will just be freed in sendit(). */
  732. return ((*pru->pru_send)(so, flags_arg | PRUS_IPV6, m,
  733. (struct sockaddr *)sin6, control, td));
  734. }
  735. } else
  736. #endif
  737. if (sin6 && IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
  738. /*
  739. * Given this is either an IPv6-only socket or no INET is
  740. * supported we will fail the send if the given destination
  741. * address is a v4mapped address.
  742. *
  743. * XXXGL: do we leak m and control?
  744. */
  745. INP_UNLOCK(inp);
  746. return (EINVAL);
  747. }
  748. if (control) {
  749. if ((error = ip6_setpktopts(control, &opt,
  750. inp->in6p_outputopts, td->td_ucred, nxt)) != 0) {
  751. INP_UNLOCK(inp);
  752. ip6_clearpktopts(&opt, -1);
  753. if (control)
  754. m_freem(control);
  755. m_freem(m);
  756. return (error);
  757. }
  758. optp = &opt;
  759. } else
  760. optp = inp->in6p_outputopts;
  761. NET_EPOCH_ENTER(et);
  762. if (sin6) {
  763. /*
  764. * Since we saw no essential reason for calling in_pcbconnect,
  765. * we get rid of such kind of logic, and call in6_selectsrc
  766. * and in6_pcbsetport in order to fill in the local address
  767. * and the local port.
  768. */
  769. if (sin6->sin6_port == 0) {
  770. error = EADDRNOTAVAIL;
  771. goto release;
  772. }
  773. if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
  774. /* how about ::ffff:0.0.0.0 case? */
  775. error = EISCONN;
  776. goto release;
  777. }
  778. /*
  779. * Given we handle the v4mapped case in the INET block above
  780. * assert here that it must not happen anymore.
  781. */
  782. KASSERT(!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr),
  783. ("%s: sin6(%p)->sin6_addr is v4mapped which we "
  784. "should have handled.", __func__, sin6));
  785. /* This only requires read-locking. */
  786. error = in6_selectsrc_socket(sin6, optp, inp,
  787. td->td_ucred, scope_ambiguous, &in6a, NULL);
  788. if (error)
  789. goto release;
  790. laddr = &in6a;
  791. if (inp->inp_lport == 0) {
  792. struct inpcbinfo *pcbinfo;
  793. INP_WLOCK_ASSERT(inp);
  794. pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
  795. INP_HASH_WLOCK(pcbinfo);
  796. error = in6_pcbsetport(laddr, inp, td->td_ucred);
  797. INP_HASH_WUNLOCK(pcbinfo);
  798. if (error != 0) {
  799. /* Undo an address bind that may have occurred. */
  800. inp->in6p_laddr = in6addr_any;
  801. goto release;
  802. }
  803. }
  804. faddr = &sin6->sin6_addr;
  805. fport = sin6->sin6_port; /* allow 0 port */
  806. } else {
  807. if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
  808. error = ENOTCONN;
  809. goto release;
  810. }
  811. laddr = &inp->in6p_laddr;
  812. faddr = &inp->in6p_faddr;
  813. fport = inp->inp_fport;
  814. }
  815. ulen = m->m_pkthdr.len;
  816. plen = sizeof(struct udphdr) + ulen;
  817. hlen = sizeof(struct ip6_hdr);
  818. /*
  819. * Calculate data length and get a mbuf
  820. * for UDP and IP6 headers.
  821. */
  822. M_PREPEND(m, hlen + sizeof(struct udphdr), M_NOWAIT);
  823. if (m == NULL) {
  824. error = ENOBUFS;
  825. goto release;
  826. }
  827. /*
  828. * Stuff checksum and output datagram.
  829. */
  830. cscov = cscov_partial = 0;
  831. udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
  832. udp6->uh_sport = inp->inp_lport; /* lport is always set in the PCB */
  833. udp6->uh_dport = fport;
  834. if (nxt == IPPROTO_UDPLITE) {
  835. struct udpcb *up;
  836. up = intoudpcb(inp);
  837. cscov = up->u_txcslen;
  838. if (cscov >= plen)
  839. cscov = 0;
  840. udp6->uh_ulen = htons(cscov);
  841. /*
  842. * For UDP-Lite, checksum coverage length of zero means
  843. * the entire UDPLite packet is covered by the checksum.
  844. */
  845. cscov_partial = (cscov == 0) ? 0 : 1;
  846. } else if (plen <= 0xffff)
  847. udp6->uh_ulen = htons((u_short)plen);
  848. else
  849. udp6->uh_ulen = 0;
  850. udp6->uh_sum = 0;
  851. ip6 = mtod(m, struct ip6_hdr *);
  852. ip6->ip6_flow = inp->inp_flow & IPV6_FLOWINFO_MASK;
  853. ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
  854. ip6->ip6_vfc |= IPV6_VERSION;
  855. ip6->ip6_plen = htons((u_short)plen);
  856. ip6->ip6_nxt = nxt;
  857. ip6->ip6_hlim = in6_selecthlim(inp, NULL);
  858. ip6->ip6_src = *laddr;
  859. ip6->ip6_dst = *faddr;
  860. #ifdef MAC
  861. mac_inpcb_create_mbuf(inp, m);
  862. #endif
  863. if (cscov_partial) {
  864. if ((udp6->uh_sum = in6_cksum_partial(m, nxt,
  865. sizeof(struct ip6_hdr), plen, cscov)) == 0)
  866. udp6->uh_sum = 0xffff;
  867. } else {
  868. udp6->uh_sum = in6_cksum_pseudo(ip6, plen, nxt, 0);
  869. m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
  870. m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
  871. }
  872. flags = 0;
  873. #if defined(ROUTE_MPATH) || defined(RSS)
  874. if (CALC_FLOWID_OUTBOUND_SENDTO) {
  875. uint32_t hash_type, hash_val;
  876. uint8_t pr;
  877. pr = inp->inp_socket->so_proto->pr_protocol;
  878. hash_val = fib6_calc_packet_hash(laddr, faddr,
  879. inp->inp_lport, fport, pr, &hash_type);
  880. m->m_pkthdr.flowid = hash_val;
  881. M_HASHTYPE_SET(m, hash_type);
  882. }
  883. /* do not use inp flowid */
  884. flags |= IP_NODEFAULTFLOWID;
  885. #endif
  886. UDPSTAT_INC(udps_opackets);
  887. if (nxt == IPPROTO_UDPLITE)
  888. UDPLITE_PROBE(send, NULL, inp, ip6, inp, udp6);
  889. else
  890. UDP_PROBE(send, NULL, inp, ip6, inp, udp6);
  891. error = ip6_output(m, optp,
  892. INP_WLOCKED(inp) ? &inp->inp_route6 : NULL, flags,
  893. inp->in6p_moptions, NULL, inp);
  894. INP_UNLOCK(inp);
  895. NET_EPOCH_EXIT(et);
  896. if (control) {
  897. ip6_clearpktopts(&opt, -1);
  898. m_freem(control);
  899. }
  900. return (error);
  901. release:
  902. INP_UNLOCK(inp);
  903. NET_EPOCH_EXIT(et);
  904. if (control) {
  905. ip6_clearpktopts(&opt, -1);
  906. m_freem(control);
  907. }
  908. m_freem(m);
  909. return (error);
  910. }
  911. static void
  912. udp6_abort(struct socket *so)
  913. {
  914. struct inpcb *inp;
  915. struct inpcbinfo *pcbinfo;
  916. pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
  917. inp = sotoinpcb(so);
  918. KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
  919. INP_WLOCK(inp);
  920. #ifdef INET
  921. if (inp->inp_vflag & INP_IPV4) {
  922. struct pr_usrreqs *pru;
  923. uint8_t nxt;
  924. nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
  925. IPPROTO_UDP : IPPROTO_UDPLITE;
  926. INP_WUNLOCK(inp);
  927. pru = inetsw[ip_protox[nxt]].pr_usrreqs;
  928. (*pru->pru_abort)(so);
  929. return;
  930. }
  931. #endif
  932. if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
  933. INP_HASH_WLOCK(pcbinfo);
  934. in6_pcbdisconnect(inp);
  935. inp->in6p_laddr = in6addr_any;
  936. INP_HASH_WUNLOCK(pcbinfo);
  937. soisdisconnected(so);
  938. }
  939. INP_WUNLOCK(inp);
  940. }
  941. static int
  942. udp6_attach(struct socket *so, int proto, struct thread *td)
  943. {
  944. struct inpcb *inp;
  945. struct inpcbinfo *pcbinfo;
  946. int error;
  947. pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
  948. inp = sotoinpcb(so);
  949. KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
  950. if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
  951. error = soreserve(so, udp_sendspace, udp_recvspace);
  952. if (error)
  953. return (error);
  954. }
  955. INP_INFO_WLOCK(pcbinfo);
  956. error = in_pcballoc(so, pcbinfo);
  957. if (error) {
  958. INP_INFO_WUNLOCK(pcbinfo);
  959. return (error);
  960. }
  961. inp = (struct inpcb *)so->so_pcb;
  962. inp->inp_vflag |= INP_IPV6;
  963. if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
  964. inp->inp_vflag |= INP_IPV4;
  965. inp->in6p_hops = -1; /* use kernel default */
  966. inp->in6p_cksum = -1; /* just to be sure */
  967. /*
  968. * XXX: ugly!!
  969. * IPv4 TTL initialization is necessary for an IPv6 socket as well,
  970. * because the socket may be bound to an IPv6 wildcard address,
  971. * which may match an IPv4-mapped IPv6 address.
  972. */
  973. inp->inp_ip_ttl = V_ip_defttl;
  974. error = udp_newudpcb(inp);
  975. if (error) {
  976. in_pcbdetach(inp);
  977. in_pcbfree(inp);
  978. INP_INFO_WUNLOCK(pcbinfo);
  979. return (error);
  980. }
  981. INP_WUNLOCK(inp);
  982. INP_INFO_WUNLOCK(pcbinfo);
  983. return (0);
  984. }
  985. static int
  986. udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
  987. {
  988. struct inpcb *inp;
  989. struct inpcbinfo *pcbinfo;
  990. int error;
  991. u_char vflagsav;
  992. pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
  993. inp = sotoinpcb(so);
  994. KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
  995. INP_WLOCK(inp);
  996. INP_HASH_WLOCK(pcbinfo);
  997. vflagsav = inp->inp_vflag;
  998. inp->inp_vflag &= ~INP_IPV4;
  999. inp->inp_vflag |= INP_IPV6;
  1000. if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
  1001. struct sockaddr_in6 *sin6_p;
  1002. sin6_p = (struct sockaddr_in6 *)nam;
  1003. if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
  1004. inp->inp_vflag |= INP_IPV4;
  1005. #ifdef INET
  1006. else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
  1007. struct sockaddr_in sin;
  1008. in6_sin6_2_sin(&sin, sin6_p);
  1009. inp->inp_vflag |= INP_IPV4;
  1010. inp->inp_vflag &= ~INP_IPV6;
  1011. error = in_pcbbind(inp, (struct sockaddr *)&sin,
  1012. td->td_ucred);
  1013. goto out;
  1014. }
  1015. #endif
  1016. }
  1017. error = in6_pcbbind(inp, nam, td->td_ucred);
  1018. #ifdef INET
  1019. out:
  1020. #endif
  1021. if (error != 0)
  1022. inp->inp_vflag = vflagsav;
  1023. INP_HASH_WUNLOCK(pcbinfo);
  1024. INP_WUNLOCK(inp);
  1025. return (error);
  1026. }
  1027. static void
  1028. udp6_close(struct socket *so)
  1029. {
  1030. struct inpcb *inp;
  1031. struct inpcbinfo *pcbinfo;
  1032. pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
  1033. inp = sotoinpcb(so);
  1034. KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
  1035. INP_WLOCK(inp);
  1036. #ifdef INET
  1037. if (inp->inp_vflag & INP_IPV4) {
  1038. struct pr_usrreqs *pru;
  1039. uint8_t nxt;
  1040. nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
  1041. IPPROTO_UDP : IPPROTO_UDPLITE;
  1042. INP_WUNLOCK(inp);
  1043. pru = inetsw[ip_protox[nxt]].pr_usrreqs;
  1044. (*pru->pru_disconnect)(so);
  1045. return;
  1046. }
  1047. #endif
  1048. if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
  1049. INP_HASH_WLOCK(pcbinfo);
  1050. in6_pcbdisconnect(inp);
  1051. inp->in6p_laddr = in6addr_any;
  1052. INP_HASH_WUNLOCK(pcbinfo);
  1053. soisdisconnected(so);
  1054. }
  1055. INP_WUNLOCK(inp);
  1056. }
  1057. static int
  1058. udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
  1059. {
  1060. #ifdef INET
  1061. struct epoch_tracker et;
  1062. #endif
  1063. struct inpcb *inp;
  1064. struct inpcbinfo *pcbinfo;
  1065. struct sockaddr_in6 *sin6;
  1066. int error;
  1067. u_char vflagsav;
  1068. pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
  1069. inp = sotoinpcb(so);
  1070. sin6 = (struct sockaddr_in6 *)nam;
  1071. KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
  1072. /*
  1073. * XXXRW: Need to clarify locking of v4/v6 flags.
  1074. */
  1075. INP_WLOCK(inp);
  1076. #ifdef INET
  1077. if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
  1078. struct sockaddr_in sin;
  1079. if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
  1080. error = EINVAL;
  1081. goto out;
  1082. }
  1083. if ((inp->inp_vflag & INP_IPV4) == 0) {
  1084. error = EAFNOSUPPORT;
  1085. goto out;
  1086. }
  1087. if (inp->inp_faddr.s_addr != INADDR_ANY) {
  1088. error = EISCONN;
  1089. goto out;
  1090. }
  1091. in6_sin6_2_sin(&sin, sin6);
  1092. error = prison_remote_ip4(td->td_ucred, &sin.sin_addr);
  1093. if (error != 0)
  1094. goto out;
  1095. vflagsav = inp->inp_vflag;
  1096. inp->inp_vflag |= INP_IPV4;
  1097. inp->inp_vflag &= ~INP_IPV6;
  1098. NET_EPOCH_ENTER(et);
  1099. INP_HASH_WLOCK(pcbinfo);
  1100. error = in_pcbconnect(inp, (struct sockaddr *)&sin,
  1101. td->td_ucred);
  1102. INP_HASH_WUNLOCK(pcbinfo);
  1103. NET_EPOCH_EXIT(et);
  1104. /*
  1105. * If connect succeeds, mark socket as connected. If
  1106. * connect fails and socket is unbound, reset inp_vflag
  1107. * field.
  1108. */
  1109. if (error == 0)
  1110. soisconnected(so);
  1111. else if (inp->inp_laddr.s_addr == INADDR_ANY &&
  1112. inp->inp_lport == 0)
  1113. inp->inp_vflag = vflagsav;
  1114. goto out;
  1115. } else {
  1116. if ((inp->inp_vflag & INP_IPV6) == 0) {
  1117. error = EAFNOSUPPORT;
  1118. goto out;
  1119. }
  1120. }
  1121. #endif
  1122. if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
  1123. error = EISCONN;
  1124. goto out;
  1125. }
  1126. error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr);
  1127. if (error != 0)
  1128. goto out;
  1129. vflagsav = inp->inp_vflag;
  1130. inp->inp_vflag &= ~INP_IPV4;
  1131. inp->inp_vflag |= INP_IPV6;
  1132. INP_HASH_WLOCK(pcbinfo);
  1133. error = in6_pcbconnect(inp, nam, td->td_ucred);
  1134. INP_HASH_WUNLOCK(pcbinfo);
  1135. /*
  1136. * If connect succeeds, mark socket as connected. If
  1137. * connect fails and socket is unbound, reset inp_vflag
  1138. * field.
  1139. */
  1140. if (error == 0)
  1141. soisconnected(so);
  1142. else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
  1143. inp->inp_lport == 0)
  1144. inp->inp_vflag = vflagsav;
  1145. out:
  1146. INP_WUNLOCK(inp);
  1147. return (error);
  1148. }
  1149. static void
  1150. udp6_detach(struct socket *so)
  1151. {
  1152. struct inpcb *inp;
  1153. struct inpcbinfo *pcbinfo;
  1154. struct udpcb *up;
  1155. pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
  1156. inp = sotoinpcb(so);
  1157. KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
  1158. INP_INFO_WLOCK(pcbinfo);
  1159. INP_WLOCK(inp);
  1160. up = intoudpcb(inp);
  1161. KASSERT(up != NULL, ("%s: up == NULL", __func__));
  1162. in_pcbdetach(inp);
  1163. in_pcbfree(inp);
  1164. INP_INFO_WUNLOCK(pcbinfo);
  1165. udp_discardcb(up);
  1166. }
  1167. static int
  1168. udp6_disconnect(struct socket *so)
  1169. {
  1170. struct inpcb *inp;
  1171. struct inpcbinfo *pcbinfo;
  1172. pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
  1173. inp = sotoinpcb(so);
  1174. KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
  1175. INP_WLOCK(inp);
  1176. #ifdef INET
  1177. if (inp->inp_vflag & INP_IPV4) {
  1178. struct pr_usrreqs *pru;
  1179. uint8_t nxt;
  1180. nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
  1181. IPPROTO_UDP : IPPROTO_UDPLITE;
  1182. INP_WUNLOCK(inp);
  1183. pru = inetsw[ip_protox[nxt]].pr_usrreqs;
  1184. (void)(*pru->pru_disconnect)(so);
  1185. return (0);
  1186. }
  1187. #endif
  1188. if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
  1189. INP_WUNLOCK(inp);
  1190. return (ENOTCONN);
  1191. }
  1192. INP_HASH_WLOCK(pcbinfo);
  1193. in6_pcbdisconnect(inp);
  1194. inp->in6p_laddr = in6addr_any;
  1195. INP_HASH_WUNLOCK(pcbinfo);
  1196. SOCK_LOCK(so);
  1197. so->so_state &= ~SS_ISCONNECTED; /* XXX */
  1198. SOCK_UNLOCK(so);
  1199. INP_WUNLOCK(inp);
  1200. return (0);
  1201. }
  1202. static int
  1203. udp6_send(struct socket *so, int flags, struct mbuf *m,
  1204. struct sockaddr *addr, struct mbuf *control, struct thread *td)
  1205. {
  1206. int error;
  1207. if (addr) {
  1208. if (addr->sa_len != sizeof(struct sockaddr_in6)) {
  1209. error = EINVAL;
  1210. goto bad;
  1211. }
  1212. if (addr->sa_family != AF_INET6) {
  1213. error = EAFNOSUPPORT;
  1214. goto bad;
  1215. }
  1216. }
  1217. return (udp6_output(so, flags, m, addr, control, td));
  1218. bad:
  1219. if (control)
  1220. m_freem(control);
  1221. m_freem(m);
  1222. return (error);
  1223. }
  1224. struct pr_usrreqs udp6_usrreqs = {
  1225. .pru_abort = udp6_abort,
  1226. .pru_attach = udp6_attach,
  1227. .pru_bind = udp6_bind,
  1228. .pru_connect = udp6_connect,
  1229. .pru_control = in6_control,
  1230. .pru_detach = udp6_detach,
  1231. .pru_disconnect = udp6_disconnect,
  1232. .pru_peeraddr = in6_mapped_peeraddr,
  1233. .pru_send = udp6_send,
  1234. .pru_shutdown = udp_shutdown,
  1235. .pru_sockaddr = in6_mapped_sockaddr,
  1236. .pru_soreceive = soreceive_dgram,
  1237. .pru_sosend = sosend_dgram,
  1238. .pru_sosetlabel = in_pcbsosetlabel,
  1239. .pru_close = udp6_close
  1240. };