ip6_output.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244
  1. /* $OpenBSD: ip6_output.c,v 1.177 2015/07/16 21:14:21 mpi Exp $ */
  2. /* $KAME: ip6_output.c,v 1.172 2001/03/25 09:55:56 itojun Exp $ */
  3. /*
  4. * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the project nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. */
  31. /*
  32. * Copyright (c) 1982, 1986, 1988, 1990, 1993
  33. * The Regents of the University of California. All rights reserved.
  34. *
  35. * Redistribution and use in source and binary forms, with or without
  36. * modification, are permitted provided that the following conditions
  37. * are met:
  38. * 1. Redistributions of source code must retain the above copyright
  39. * notice, this list of conditions and the following disclaimer.
  40. * 2. Redistributions in binary form must reproduce the above copyright
  41. * notice, this list of conditions and the following disclaimer in the
  42. * documentation and/or other materials provided with the distribution.
  43. * 3. Neither the name of the University nor the names of its contributors
  44. * may be used to endorse or promote products derived from this software
  45. * without specific prior written permission.
  46. *
  47. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  48. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  49. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  50. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  51. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  52. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  53. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  54. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  55. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  56. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  57. * SUCH DAMAGE.
  58. *
  59. * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
  60. */
  61. #include "pf.h"
  62. #include <sys/param.h>
  63. #include <sys/malloc.h>
  64. #include <sys/mbuf.h>
  65. #include <sys/errno.h>
  66. #include <sys/protosw.h>
  67. #include <sys/socket.h>
  68. #include <sys/socketvar.h>
  69. #include <sys/proc.h>
  70. #include <sys/systm.h>
  71. #include <net/if.h>
  72. #include <net/if_var.h>
  73. #include <net/if_enc.h>
  74. #include <net/route.h>
  75. #include <netinet/in.h>
  76. #include <netinet/ip.h>
  77. #include <netinet/in_pcb.h>
  78. #include <netinet/udp.h>
  79. #include <netinet/tcp.h>
  80. #include <netinet/ip_var.h>
  81. #include <netinet/tcp_timer.h>
  82. #include <netinet/tcp_var.h>
  83. #include <netinet/udp_var.h>
  84. #include <netinet6/in6_var.h>
  85. #include <netinet/ip6.h>
  86. #include <netinet/icmp6.h>
  87. #include <netinet6/ip6_var.h>
  88. #include <netinet6/nd6.h>
  89. #include <netinet6/ip6protosw.h>
  90. #include <crypto/idgen.h>
  91. #if NPF > 0
  92. #include <net/pfvar.h>
  93. #endif
  94. #ifdef IPSEC
  95. #include <netinet/ip_ipsp.h>
  96. #include <netinet/ip_ah.h>
  97. #include <netinet/ip_esp.h>
  98. #endif /* IPSEC */
  99. struct ip6_exthdrs {
  100. struct mbuf *ip6e_ip6;
  101. struct mbuf *ip6e_hbh;
  102. struct mbuf *ip6e_dest1;
  103. struct mbuf *ip6e_rthdr;
  104. struct mbuf *ip6e_dest2;
  105. };
  106. int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **, int, int);
  107. int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *, struct socket *);
  108. int ip6_getpcbopt(struct ip6_pktopts *, int, struct mbuf **);
  109. int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *, int, int,
  110. int, int);
  111. int ip6_setmoptions(int, struct ip6_moptions **, struct mbuf *);
  112. int ip6_getmoptions(int, struct ip6_moptions *, struct mbuf **);
  113. int ip6_copyexthdr(struct mbuf **, caddr_t, int);
  114. int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
  115. struct ip6_frag **);
  116. int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
  117. int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
  118. int ip6_getpmtu(struct route_in6 *, struct route_in6 *,
  119. struct ifnet *, struct in6_addr *, u_long *, int *);
  120. int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
  121. static __inline u_int16_t __attribute__((__unused__))
  122. in6_cksum_phdr(const struct in6_addr *, const struct in6_addr *,
  123. u_int32_t, u_int32_t);
  124. void in6_delayed_cksum(struct mbuf *, u_int8_t);
  125. /* Context for non-repeating IDs */
  126. struct idgen32_ctx ip6_id_ctx;
  127. /*
  128. * IP6 output. The packet in mbuf chain m contains a skeletal IP6
  129. * header (with pri, len, nxt, hlim, src, dst).
  130. * This function may modify ver and hlim only.
  131. * The mbuf chain containing the packet will be freed.
  132. * The mbuf opt, if present, will not be freed.
  133. *
  134. * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
  135. * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
  136. * which is rt_rmx.rmx_mtu.
  137. *
  138. * ifpp - XXX: just for statistics
  139. */
  140. int
  141. ip6_output(struct mbuf *m0, struct ip6_pktopts *opt, struct route_in6 *ro,
  142. int flags, struct ip6_moptions *im6o, struct ifnet **ifpp,
  143. struct inpcb *inp)
  144. {
  145. struct ip6_hdr *ip6;
  146. struct ifnet *ifp;
  147. struct mbuf *m = m0;
  148. int hlen, tlen;
  149. struct route_in6 ip6route;
  150. struct rtentry *rt = NULL;
  151. struct sockaddr_in6 *dst, dstsock;
  152. int error = 0;
  153. u_long mtu;
  154. int alwaysfrag, dontfrag;
  155. u_int16_t src_scope, dst_scope;
  156. u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
  157. struct ip6_exthdrs exthdrs;
  158. struct in6_addr finaldst;
  159. struct route_in6 *ro_pmtu = NULL;
  160. int hdrsplit = 0;
  161. u_int8_t sproto = 0;
  162. #ifdef IPSEC
  163. struct m_tag *mtag;
  164. union sockaddr_union sdst;
  165. struct tdb_ident *tdbi;
  166. u_int32_t sspi;
  167. struct tdb *tdb;
  168. #if NPF > 0
  169. struct ifnet *encif;
  170. #endif
  171. #endif /* IPSEC */
  172. #ifdef IPSEC
  173. if (inp && (inp->inp_flags & INP_IPV6) == 0)
  174. panic("ip6_output: IPv4 pcb is passed");
  175. #endif /* IPSEC */
  176. ip6 = mtod(m, struct ip6_hdr *);
  177. finaldst = ip6->ip6_dst;
  178. #define MAKE_EXTHDR(hp, mp) \
  179. do { \
  180. if (hp) { \
  181. struct ip6_ext *eh = (struct ip6_ext *)(hp); \
  182. error = ip6_copyexthdr((mp), (caddr_t)(hp), \
  183. ((eh)->ip6e_len + 1) << 3); \
  184. if (error) \
  185. goto freehdrs; \
  186. } \
  187. } while (0)
  188. bzero(&exthdrs, sizeof(exthdrs));
  189. if (opt) {
  190. /* Hop-by-Hop options header */
  191. MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
  192. /* Destination options header(1st part) */
  193. MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
  194. /* Routing header */
  195. MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
  196. /* Destination options header(2nd part) */
  197. MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
  198. }
  199. #ifdef IPSEC
  200. if (!ipsec_in_use && !inp)
  201. goto done_spd;
  202. /*
  203. * Check if there was an outgoing SA bound to the flow
  204. * from a transport protocol.
  205. */
  206. ip6 = mtod(m, struct ip6_hdr *);
  207. /* Do we have any pending SAs to apply ? */
  208. tdb = ipsp_spd_lookup(m, AF_INET6, sizeof(struct ip6_hdr),
  209. &error, IPSP_DIRECTION_OUT, NULL, inp, 0);
  210. if (tdb == NULL) {
  211. if (error == 0) {
  212. /*
  213. * No IPsec processing required, we'll just send the
  214. * packet out.
  215. */
  216. sproto = 0;
  217. /* Fall through to routing/multicast handling */
  218. } else {
  219. /*
  220. * -EINVAL is used to indicate that the packet should
  221. * be silently dropped, typically because we've asked
  222. * key management for an SA.
  223. */
  224. if (error == -EINVAL) /* Should silently drop packet */
  225. error = 0;
  226. goto freehdrs;
  227. }
  228. } else {
  229. /* Loop detection */
  230. for (mtag = m_tag_first(m); mtag != NULL;
  231. mtag = m_tag_next(m, mtag)) {
  232. if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE)
  233. continue;
  234. tdbi = (struct tdb_ident *)(mtag + 1);
  235. if (tdbi->spi == tdb->tdb_spi &&
  236. tdbi->proto == tdb->tdb_sproto &&
  237. tdbi->rdomain == tdb->tdb_rdomain &&
  238. !bcmp(&tdbi->dst, &tdb->tdb_dst,
  239. sizeof(union sockaddr_union))) {
  240. sproto = 0; /* mark as no-IPsec-needed */
  241. goto done_spd;
  242. }
  243. }
  244. /* We need to do IPsec */
  245. bcopy(&tdb->tdb_dst, &sdst, sizeof(sdst));
  246. sspi = tdb->tdb_spi;
  247. sproto = tdb->tdb_sproto;
  248. }
  249. /* Fall through to the routing/multicast handling code */
  250. done_spd:
  251. #endif /* IPSEC */
  252. /*
  253. * Calculate the total length of the extension header chain.
  254. * Keep the length of the unfragmentable part for fragmentation.
  255. */
  256. optlen = 0;
  257. if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len;
  258. if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len;
  259. if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len;
  260. unfragpartlen = optlen + sizeof(struct ip6_hdr);
  261. /* NOTE: we don't add AH/ESP length here. do that later. */
  262. if (exthdrs.ip6e_dest2) optlen += exthdrs.ip6e_dest2->m_len;
  263. /*
  264. * If we need IPsec, or there is at least one extension header,
  265. * separate IP6 header from the payload.
  266. */
  267. if ((sproto || optlen) && !hdrsplit) {
  268. if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
  269. m = NULL;
  270. goto freehdrs;
  271. }
  272. m = exthdrs.ip6e_ip6;
  273. hdrsplit++;
  274. }
  275. /* adjust pointer */
  276. ip6 = mtod(m, struct ip6_hdr *);
  277. /* adjust mbuf packet header length */
  278. m->m_pkthdr.len += optlen;
  279. plen = m->m_pkthdr.len - sizeof(*ip6);
  280. /* If this is a jumbo payload, insert a jumbo payload option. */
  281. if (plen > IPV6_MAXPACKET) {
  282. if (!hdrsplit) {
  283. if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
  284. m = NULL;
  285. goto freehdrs;
  286. }
  287. m = exthdrs.ip6e_ip6;
  288. hdrsplit++;
  289. }
  290. /* adjust pointer */
  291. ip6 = mtod(m, struct ip6_hdr *);
  292. if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
  293. goto freehdrs;
  294. ip6->ip6_plen = 0;
  295. } else
  296. ip6->ip6_plen = htons(plen);
  297. /*
  298. * Concatenate headers and fill in next header fields.
  299. * Here we have, on "m"
  300. * IPv6 payload
  301. * and we insert headers accordingly. Finally, we should be getting:
  302. * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
  303. *
  304. * during the header composing process, "m" points to IPv6 header.
  305. * "mprev" points to an extension header prior to esp.
  306. */
  307. {
  308. u_char *nexthdrp = &ip6->ip6_nxt;
  309. struct mbuf *mprev = m;
  310. /*
  311. * we treat dest2 specially. this makes IPsec processing
  312. * much easier. the goal here is to make mprev point the
  313. * mbuf prior to dest2.
  314. *
  315. * result: IPv6 dest2 payload
  316. * m and mprev will point to IPv6 header.
  317. */
  318. if (exthdrs.ip6e_dest2) {
  319. if (!hdrsplit)
  320. panic("assumption failed: hdr not split");
  321. exthdrs.ip6e_dest2->m_next = m->m_next;
  322. m->m_next = exthdrs.ip6e_dest2;
  323. *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
  324. ip6->ip6_nxt = IPPROTO_DSTOPTS;
  325. }
  326. #define MAKE_CHAIN(m, mp, p, i)\
  327. do {\
  328. if (m) {\
  329. if (!hdrsplit) \
  330. panic("assumption failed: hdr not split"); \
  331. *mtod((m), u_char *) = *(p);\
  332. *(p) = (i);\
  333. p = mtod((m), u_char *);\
  334. (m)->m_next = (mp)->m_next;\
  335. (mp)->m_next = (m);\
  336. (mp) = (m);\
  337. }\
  338. } while (0)
  339. /*
  340. * result: IPv6 hbh dest1 rthdr dest2 payload
  341. * m will point to IPv6 header. mprev will point to the
  342. * extension header prior to dest2 (rthdr in the above case).
  343. */
  344. MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
  345. MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
  346. IPPROTO_DSTOPTS);
  347. MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
  348. IPPROTO_ROUTING);
  349. }
  350. /*
  351. * If there is a routing header, replace the destination address field
  352. * with the first hop of the routing header.
  353. */
  354. if (exthdrs.ip6e_rthdr) {
  355. struct ip6_rthdr *rh;
  356. struct ip6_rthdr0 *rh0;
  357. struct in6_addr *addr;
  358. rh = (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr,
  359. struct ip6_rthdr *));
  360. switch (rh->ip6r_type) {
  361. case IPV6_RTHDR_TYPE_0:
  362. rh0 = (struct ip6_rthdr0 *)rh;
  363. addr = (struct in6_addr *)(rh0 + 1);
  364. ip6->ip6_dst = addr[0];
  365. bcopy(&addr[1], &addr[0],
  366. sizeof(struct in6_addr) * (rh0->ip6r0_segleft - 1));
  367. addr[rh0->ip6r0_segleft - 1] = finaldst;
  368. break;
  369. default: /* is it possible? */
  370. error = EINVAL;
  371. goto bad;
  372. }
  373. }
  374. /* Source address validation */
  375. if (!(flags & IPV6_UNSPECSRC) &&
  376. IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
  377. /*
  378. * XXX: we can probably assume validation in the caller, but
  379. * we explicitly check the address here for safety.
  380. */
  381. error = EOPNOTSUPP;
  382. ip6stat.ip6s_badscope++;
  383. goto bad;
  384. }
  385. if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
  386. error = EOPNOTSUPP;
  387. ip6stat.ip6s_badscope++;
  388. goto bad;
  389. }
  390. ip6stat.ip6s_localout++;
  391. /*
  392. * Route packet.
  393. */
  394. #if NPF > 0
  395. reroute:
  396. #endif
  397. /* initialize cached route */
  398. if (ro == NULL) {
  399. ro = &ip6route;
  400. bzero((caddr_t)ro, sizeof(*ro));
  401. }
  402. ro_pmtu = ro;
  403. if (opt && opt->ip6po_rthdr)
  404. ro = &opt->ip6po_route;
  405. dst = &ro->ro_dst;
  406. /*
  407. * if specified, try to fill in the traffic class field.
  408. * do not override if a non-zero value is already set.
  409. * we check the diffserv field and the ecn field separately.
  410. */
  411. if (opt && opt->ip6po_tclass >= 0) {
  412. int mask = 0;
  413. if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
  414. mask |= 0xfc;
  415. if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
  416. mask |= 0x03;
  417. if (mask != 0)
  418. ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
  419. }
  420. /* fill in or override the hop limit field, if necessary. */
  421. if (opt && opt->ip6po_hlim != -1)
  422. ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
  423. else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
  424. if (im6o != NULL)
  425. ip6->ip6_hlim = im6o->im6o_hlim;
  426. else
  427. ip6->ip6_hlim = ip6_defmcasthlim;
  428. }
  429. #ifdef IPSEC
  430. /*
  431. * Check if the packet needs encapsulation.
  432. * ipsp_process_packet will never come back to here.
  433. */
  434. if (sproto != 0) {
  435. /*
  436. * XXX what should we do if ip6_hlim == 0 and the
  437. * packet gets tunneled?
  438. */
  439. tdb = gettdb(rtable_l2(m->m_pkthdr.ph_rtableid),
  440. sspi, &sdst, sproto);
  441. if (tdb == NULL) {
  442. error = EHOSTUNREACH;
  443. m_freem(m);
  444. goto done;
  445. }
  446. #if NPF > 0
  447. if ((encif = enc_getif(tdb->tdb_rdomain,
  448. tdb->tdb_tap)) == NULL ||
  449. pf_test(AF_INET6, PF_OUT, encif, &m) != PF_PASS) {
  450. error = EHOSTUNREACH;
  451. m_freem(m);
  452. goto done;
  453. }
  454. if (m == NULL)
  455. goto done;
  456. ip6 = mtod(m, struct ip6_hdr *);
  457. /*
  458. * PF_TAG_REROUTE handling or not...
  459. * Packet is entering IPsec so the routing is
  460. * already overruled by the IPsec policy.
  461. * Until now the change was not reconsidered.
  462. * What's the behaviour?
  463. */
  464. #endif
  465. in6_proto_cksum_out(m, encif);
  466. m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
  467. /* Callee frees mbuf */
  468. /*
  469. * if we are source-routing, do not attempt to tunnel the
  470. * packet just because ip6_dst is different from what tdb has.
  471. * XXX
  472. */
  473. error = ipsp_process_packet(m, tdb, AF_INET6,
  474. exthdrs.ip6e_rthdr ? 1 : 0);
  475. return error; /* Nothing more to be done */
  476. }
  477. #endif /* IPSEC */
  478. bzero(&dstsock, sizeof(dstsock));
  479. dstsock.sin6_family = AF_INET6;
  480. dstsock.sin6_addr = ip6->ip6_dst;
  481. dstsock.sin6_len = sizeof(dstsock);
  482. ro->ro_tableid = m->m_pkthdr.ph_rtableid;
  483. if ((error = in6_selectroute(&dstsock, opt, im6o, ro, &ifp,
  484. &rt, m->m_pkthdr.ph_rtableid)) != 0) {
  485. switch (error) {
  486. case EHOSTUNREACH:
  487. ip6stat.ip6s_noroute++;
  488. break;
  489. case EADDRNOTAVAIL:
  490. default:
  491. break; /* XXX statistics? */
  492. }
  493. if (ifp != NULL)
  494. in6_ifstat_inc(ifp, ifs6_out_discard);
  495. goto bad;
  496. }
  497. if (rt == NULL) {
  498. /*
  499. * If in6_selectroute() does not return a route entry,
  500. * dst may not have been updated.
  501. */
  502. *dst = dstsock; /* XXX */
  503. }
  504. /*
  505. * then rt (for unicast) and ifp must be non-NULL valid values.
  506. */
  507. if (rt)
  508. rt->rt_use++;
  509. if ((flags & IPV6_FORWARDING) == 0) {
  510. /* XXX: the FORWARDING flag can be set for mrouting. */
  511. in6_ifstat_inc(ifp, ifs6_out_request);
  512. }
  513. if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
  514. if (opt && opt->ip6po_nextroute.ro_rt) {
  515. /*
  516. * The nexthop is explicitly specified by the
  517. * application. We assume the next hop is an IPv6
  518. * address.
  519. */
  520. dst = satosin6(opt->ip6po_nexthop);
  521. } else if ((rt->rt_flags & RTF_GATEWAY))
  522. dst = satosin6(rt->rt_gateway);
  523. }
  524. if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
  525. /* Unicast */
  526. m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
  527. } else {
  528. /* Multicast */
  529. struct in6_multi *in6m;
  530. m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
  531. in6_ifstat_inc(ifp, ifs6_out_mcast);
  532. /*
  533. * Confirm that the outgoing interface supports multicast.
  534. */
  535. if ((ifp->if_flags & IFF_MULTICAST) == 0) {
  536. ip6stat.ip6s_noroute++;
  537. in6_ifstat_inc(ifp, ifs6_out_discard);
  538. error = ENETUNREACH;
  539. goto bad;
  540. }
  541. IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
  542. if (in6m != NULL &&
  543. (im6o == NULL || im6o->im6o_loop)) {
  544. /*
  545. * If we belong to the destination multicast group
  546. * on the outgoing interface, and the caller did not
  547. * forbid loopback, loop back a copy.
  548. */
  549. ip6_mloopback(ifp, m, dst);
  550. } else {
  551. /*
  552. * If we are acting as a multicast router, perform
  553. * multicast forwarding as if the packet had just
  554. * arrived on the interface to which we are about
  555. * to send. The multicast forwarding function
  556. * recursively calls this function, using the
  557. * IPV6_FORWARDING flag to prevent infinite recursion.
  558. *
  559. * Multicasts that are looped back by ip6_mloopback(),
  560. * above, will be forwarded by the ip6_input() routine,
  561. * if necessary.
  562. */
  563. #ifdef MROUTING
  564. if (ip6_mforwarding && ip6_mrouter &&
  565. (flags & IPV6_FORWARDING) == 0) {
  566. if (ip6_mforward(ip6, ifp, m) != 0) {
  567. m_freem(m);
  568. goto done;
  569. }
  570. }
  571. #endif
  572. }
  573. /*
  574. * Multicasts with a hoplimit of zero may be looped back,
  575. * above, but must not be transmitted on a network.
  576. * Also, multicasts addressed to the loopback interface
  577. * are not sent -- the above call to ip6_mloopback() will
  578. * loop back a copy if this host actually belongs to the
  579. * destination group on the loopback interface.
  580. */
  581. if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
  582. IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
  583. m_freem(m);
  584. goto done;
  585. }
  586. }
  587. /*
  588. * If this packet is going trough a loopback interface we wont
  589. * be able to restore its scope ID using the interface index.
  590. */
  591. if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) {
  592. if (ifp->if_flags & IFF_LOOPBACK)
  593. src_scope = ip6->ip6_src.s6_addr16[1];
  594. ip6->ip6_src.s6_addr16[1] = 0;
  595. }
  596. if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) {
  597. if (ifp->if_flags & IFF_LOOPBACK)
  598. dst_scope = ip6->ip6_dst.s6_addr16[1];
  599. ip6->ip6_dst.s6_addr16[1] = 0;
  600. }
  601. /*
  602. * Fill the outgoing interface to tell the upper layer
  603. * to increment per-interface statistics.
  604. */
  605. if (ifpp)
  606. *ifpp = ifp;
  607. /* Determine path MTU. */
  608. if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
  609. &alwaysfrag)) != 0)
  610. goto bad;
  611. /*
  612. * The caller of this function may specify to use the minimum MTU
  613. * in some cases.
  614. * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
  615. * setting. The logic is a bit complicated; by default, unicast
  616. * packets will follow path MTU while multicast packets will be sent at
  617. * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
  618. * including unicast ones will be sent at the minimum MTU. Multicast
  619. * packets will always be sent at the minimum MTU unless
  620. * IP6PO_MINMTU_DISABLE is explicitly specified.
  621. * See RFC 3542 for more details.
  622. */
  623. if (mtu > IPV6_MMTU) {
  624. if ((flags & IPV6_MINMTU))
  625. mtu = IPV6_MMTU;
  626. else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
  627. mtu = IPV6_MMTU;
  628. else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
  629. (opt == NULL ||
  630. opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
  631. mtu = IPV6_MMTU;
  632. }
  633. }
  634. /*
  635. * If the outgoing packet contains a hop-by-hop options header,
  636. * it must be examined and processed even by the source node.
  637. * (RFC 2460, section 4.)
  638. */
  639. if (exthdrs.ip6e_hbh) {
  640. struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
  641. u_int32_t dummy1; /* XXX unused */
  642. u_int32_t dummy2; /* XXX unused */
  643. /*
  644. * XXX: if we have to send an ICMPv6 error to the sender,
  645. * we need the M_LOOP flag since icmp6_error() expects
  646. * the IPv6 and the hop-by-hop options header are
  647. * continuous unless the flag is set.
  648. */
  649. m->m_flags |= M_LOOP;
  650. m->m_pkthdr.ph_ifidx = ifp->if_index;
  651. if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
  652. ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
  653. &dummy1, &dummy2) < 0) {
  654. /* m was already freed at this point */
  655. error = EINVAL;/* better error? */
  656. goto done;
  657. }
  658. m->m_flags &= ~M_LOOP; /* XXX */
  659. m->m_pkthdr.ph_ifidx = 0;
  660. }
  661. #if NPF > 0
  662. if (pf_test(AF_INET6, PF_OUT, ifp, &m) != PF_PASS) {
  663. error = EHOSTUNREACH;
  664. m_freem(m);
  665. goto done;
  666. }
  667. if (m == NULL)
  668. goto done;
  669. ip6 = mtod(m, struct ip6_hdr *);
  670. if ((m->m_pkthdr.pf.flags & (PF_TAG_REROUTE | PF_TAG_GENERATED)) ==
  671. (PF_TAG_REROUTE | PF_TAG_GENERATED)) {
  672. /* already rerun the route lookup, go on */
  673. m->m_pkthdr.pf.flags &= ~(PF_TAG_GENERATED | PF_TAG_REROUTE);
  674. } else if (m->m_pkthdr.pf.flags & PF_TAG_REROUTE) {
  675. /* tag as generated to skip over pf_test on rerun */
  676. m->m_pkthdr.pf.flags |= PF_TAG_GENERATED;
  677. finaldst = ip6->ip6_dst;
  678. ro = NULL;
  679. goto reroute;
  680. }
  681. #endif
  682. /*
  683. * If the packet is not going on the wire it can be destinated
  684. * to any local address. In this case do not clear its scopes
  685. * to let ip6_input() find a matching local route.
  686. */
  687. if (ifp->if_flags & IFF_LOOPBACK) {
  688. if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src))
  689. ip6->ip6_src.s6_addr16[1] = src_scope;
  690. if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst))
  691. ip6->ip6_dst.s6_addr16[1] = dst_scope;
  692. }
  693. in6_proto_cksum_out(m, ifp);
  694. /*
  695. * Send the packet to the outgoing interface.
  696. * If necessary, do IPv6 fragmentation before sending.
  697. *
  698. * the logic here is rather complex:
  699. * 1: normal case (dontfrag == 0, alwaysfrag == 0)
  700. * 1-a: send as is if tlen <= path mtu
  701. * 1-b: fragment if tlen > path mtu
  702. *
  703. * 2: if user asks us not to fragment (dontfrag == 1)
  704. * 2-a: send as is if tlen <= interface mtu
  705. * 2-b: error if tlen > interface mtu
  706. *
  707. * 3: if we always need to attach fragment header (alwaysfrag == 1)
  708. * always fragment
  709. *
  710. * 4: if dontfrag == 1 && alwaysfrag == 1
  711. * error, as we cannot handle this conflicting request
  712. */
  713. tlen = m->m_pkthdr.len;
  714. if (opt && (opt->ip6po_flags & IP6PO_DONTFRAG))
  715. dontfrag = 1;
  716. else
  717. dontfrag = 0;
  718. if (dontfrag && alwaysfrag) { /* case 4 */
  719. /* conflicting request - can't transmit */
  720. error = EMSGSIZE;
  721. goto bad;
  722. }
  723. if (dontfrag && tlen > IN6_LINKMTU(ifp)) { /* case 2-b */
  724. /*
  725. * Even if the DONTFRAG option is specified, we cannot send the
  726. * packet when the data length is larger than the MTU of the
  727. * outgoing interface.
  728. * Notify the error by sending IPV6_PATHMTU ancillary data as
  729. * well as returning an error code (the latter is not described
  730. * in the API spec.)
  731. */
  732. #if 0
  733. u_int32_t mtu32;
  734. struct ip6ctlparam ip6cp;
  735. mtu32 = (u_int32_t)mtu;
  736. bzero(&ip6cp, sizeof(ip6cp));
  737. ip6cp.ip6c_cmdarg = (void *)&mtu32;
  738. pfctlinput2(PRC_MSGSIZE, sin6tosa(&ro_pmtu->ro_dst),
  739. (void *)&ip6cp);
  740. #endif
  741. error = EMSGSIZE;
  742. goto bad;
  743. }
  744. /*
  745. * transmit packet without fragmentation
  746. */
  747. if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
  748. error = nd6_output(ifp, m, dst, ro->ro_rt);
  749. goto done;
  750. }
  751. /*
  752. * try to fragment the packet. case 1-b and 3
  753. */
  754. if (mtu < IPV6_MMTU) {
  755. /* path MTU cannot be less than IPV6_MMTU */
  756. error = EMSGSIZE;
  757. in6_ifstat_inc(ifp, ifs6_out_fragfail);
  758. goto bad;
  759. } else if (ip6->ip6_plen == 0) {
  760. /* jumbo payload cannot be fragmented */
  761. error = EMSGSIZE;
  762. in6_ifstat_inc(ifp, ifs6_out_fragfail);
  763. goto bad;
  764. } else {
  765. u_char nextproto;
  766. #if 0
  767. struct ip6ctlparam ip6cp;
  768. u_int32_t mtu32;
  769. #endif
  770. /*
  771. * Too large for the destination or interface;
  772. * fragment if possible.
  773. * Must be able to put at least 8 bytes per fragment.
  774. */
  775. hlen = unfragpartlen;
  776. if (mtu > IPV6_MAXPACKET)
  777. mtu = IPV6_MAXPACKET;
  778. #if 0
  779. /* Notify a proper path MTU to applications. */
  780. mtu32 = (u_int32_t)mtu;
  781. bzero(&ip6cp, sizeof(ip6cp));
  782. ip6cp.ip6c_cmdarg = (void *)&mtu32;
  783. pfctlinput2(PRC_MSGSIZE, sin6tosa(&ro_pmtu->ro_dst),
  784. (void *)&ip6cp);
  785. #endif
  786. /*
  787. * Change the next header field of the last header in the
  788. * unfragmentable part.
  789. */
  790. if (exthdrs.ip6e_rthdr) {
  791. nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
  792. *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
  793. } else if (exthdrs.ip6e_dest1) {
  794. nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
  795. *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
  796. } else if (exthdrs.ip6e_hbh) {
  797. nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
  798. *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
  799. } else {
  800. nextproto = ip6->ip6_nxt;
  801. ip6->ip6_nxt = IPPROTO_FRAGMENT;
  802. }
  803. m0 = m;
  804. error = ip6_fragment(m0, hlen, nextproto, mtu);
  805. switch (error) {
  806. case 0:
  807. in6_ifstat_inc(ifp, ifs6_out_fragok);
  808. break;
  809. case EMSGSIZE:
  810. in6_ifstat_inc(ifp, ifs6_out_fragfail);
  811. break;
  812. default:
  813. ip6stat.ip6s_odropped++;
  814. break;
  815. }
  816. }
  817. /*
  818. * Remove leading garbages.
  819. */
  820. m = m0->m_nextpkt;
  821. m0->m_nextpkt = 0;
  822. m_freem(m0);
  823. for (m0 = m; m; m = m0) {
  824. m0 = m->m_nextpkt;
  825. m->m_nextpkt = 0;
  826. if (error == 0) {
  827. ip6stat.ip6s_ofragments++;
  828. in6_ifstat_inc(ifp, ifs6_out_fragcreat);
  829. error = nd6_output(ifp, m, dst, ro->ro_rt);
  830. } else
  831. m_freem(m);
  832. }
  833. if (error == 0)
  834. ip6stat.ip6s_fragmented++;
  835. done:
  836. if (ro == &ip6route && ro->ro_rt) {
  837. rtfree(ro->ro_rt);
  838. } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
  839. rtfree(ro_pmtu->ro_rt);
  840. }
  841. return (error);
  842. freehdrs:
  843. m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
  844. m_freem(exthdrs.ip6e_dest1);
  845. m_freem(exthdrs.ip6e_rthdr);
  846. m_freem(exthdrs.ip6e_dest2);
  847. /* FALLTHROUGH */
  848. bad:
  849. m_freem(m);
  850. goto done;
  851. }
  852. int
  853. ip6_fragment(struct mbuf *m0, int hlen, u_char nextproto, u_long mtu)
  854. {
  855. struct mbuf *m, **mnext, *m_frgpart;
  856. struct ip6_hdr *mhip6;
  857. struct ip6_frag *ip6f;
  858. u_int32_t id;
  859. int tlen, len, off;
  860. int error;
  861. id = htonl(ip6_randomid());
  862. mnext = &m0->m_nextpkt;
  863. *mnext = NULL;
  864. tlen = m0->m_pkthdr.len;
  865. len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
  866. if (len < 8)
  867. return (EMSGSIZE);
  868. /*
  869. * Loop through length of segment after first fragment,
  870. * make new header and copy data of each part and link onto
  871. * chain.
  872. */
  873. for (off = hlen; off < tlen; off += len) {
  874. struct mbuf *mlast;
  875. if ((m = m_gethdr(M_DONTWAIT, MT_HEADER)) == NULL)
  876. return (ENOBUFS);
  877. *mnext = m;
  878. mnext = &m->m_nextpkt;
  879. if ((error = m_dup_pkthdr(m, m0, M_DONTWAIT)) != 0)
  880. return (error);
  881. m->m_data += max_linkhdr;
  882. mhip6 = mtod(m, struct ip6_hdr *);
  883. *mhip6 = *mtod(m0, struct ip6_hdr *);
  884. m->m_len = sizeof(*mhip6);
  885. if ((error = ip6_insertfraghdr(m0, m, hlen, &ip6f)) != 0)
  886. return (error);
  887. ip6f->ip6f_offlg = htons((u_int16_t)((off - hlen) & ~7));
  888. if (off + len >= tlen)
  889. len = tlen - off;
  890. else
  891. ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
  892. mhip6->ip6_plen = htons((u_int16_t)(len + hlen +
  893. sizeof(*ip6f) - sizeof(struct ip6_hdr)));
  894. if ((m_frgpart = m_copym(m0, off, len, M_DONTWAIT)) == NULL)
  895. return (ENOBUFS);
  896. for (mlast = m; mlast->m_next; mlast = mlast->m_next)
  897. ;
  898. mlast->m_next = m_frgpart;
  899. m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
  900. ip6f->ip6f_reserved = 0;
  901. ip6f->ip6f_ident = id;
  902. ip6f->ip6f_nxt = nextproto;
  903. }
  904. return (0);
  905. }
  906. int
  907. ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
  908. {
  909. struct mbuf *m;
  910. if (hlen > MCLBYTES)
  911. return (ENOBUFS); /* XXX */
  912. MGET(m, M_DONTWAIT, MT_DATA);
  913. if (!m)
  914. return (ENOBUFS);
  915. if (hlen > MLEN) {
  916. MCLGET(m, M_DONTWAIT);
  917. if ((m->m_flags & M_EXT) == 0) {
  918. m_free(m);
  919. return (ENOBUFS);
  920. }
  921. }
  922. m->m_len = hlen;
  923. if (hdr)
  924. bcopy(hdr, mtod(m, caddr_t), hlen);
  925. *mp = m;
  926. return (0);
  927. }
  928. /*
  929. * Insert jumbo payload option.
  930. */
  931. int
  932. ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
  933. {
  934. struct mbuf *mopt;
  935. u_int8_t *optbuf;
  936. u_int32_t v;
  937. #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
  938. /*
  939. * If there is no hop-by-hop options header, allocate new one.
  940. * If there is one but it doesn't have enough space to store the
  941. * jumbo payload option, allocate a cluster to store the whole options.
  942. * Otherwise, use it to store the options.
  943. */
  944. if (exthdrs->ip6e_hbh == 0) {
  945. MGET(mopt, M_DONTWAIT, MT_DATA);
  946. if (mopt == NULL)
  947. return (ENOBUFS);
  948. mopt->m_len = JUMBOOPTLEN;
  949. optbuf = mtod(mopt, u_int8_t *);
  950. optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
  951. exthdrs->ip6e_hbh = mopt;
  952. } else {
  953. struct ip6_hbh *hbh;
  954. mopt = exthdrs->ip6e_hbh;
  955. if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
  956. /*
  957. * XXX assumption:
  958. * - exthdrs->ip6e_hbh is not referenced from places
  959. * other than exthdrs.
  960. * - exthdrs->ip6e_hbh is not an mbuf chain.
  961. */
  962. int oldoptlen = mopt->m_len;
  963. struct mbuf *n;
  964. /*
  965. * XXX: give up if the whole (new) hbh header does
  966. * not fit even in an mbuf cluster.
  967. */
  968. if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
  969. return (ENOBUFS);
  970. /*
  971. * As a consequence, we must always prepare a cluster
  972. * at this point.
  973. */
  974. MGET(n, M_DONTWAIT, MT_DATA);
  975. if (n) {
  976. MCLGET(n, M_DONTWAIT);
  977. if ((n->m_flags & M_EXT) == 0) {
  978. m_freem(n);
  979. n = NULL;
  980. }
  981. }
  982. if (!n)
  983. return (ENOBUFS);
  984. n->m_len = oldoptlen + JUMBOOPTLEN;
  985. bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
  986. oldoptlen);
  987. optbuf = mtod(n, u_int8_t *) + oldoptlen;
  988. m_freem(mopt);
  989. mopt = exthdrs->ip6e_hbh = n;
  990. } else {
  991. optbuf = mtod(mopt, u_int8_t *) + mopt->m_len;
  992. mopt->m_len += JUMBOOPTLEN;
  993. }
  994. optbuf[0] = IP6OPT_PADN;
  995. optbuf[1] = 0;
  996. /*
  997. * Adjust the header length according to the pad and
  998. * the jumbo payload option.
  999. */
  1000. hbh = mtod(mopt, struct ip6_hbh *);
  1001. hbh->ip6h_len += (JUMBOOPTLEN >> 3);
  1002. }
  1003. /* fill in the option. */
  1004. optbuf[2] = IP6OPT_JUMBO;
  1005. optbuf[3] = 4;
  1006. v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
  1007. bcopy(&v, &optbuf[4], sizeof(u_int32_t));
  1008. /* finally, adjust the packet header length */
  1009. exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
  1010. return (0);
  1011. #undef JUMBOOPTLEN
  1012. }
  1013. /*
  1014. * Insert fragment header and copy unfragmentable header portions.
  1015. */
  1016. int
  1017. ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
  1018. struct ip6_frag **frghdrp)
  1019. {
  1020. struct mbuf *n, *mlast;
  1021. if (hlen > sizeof(struct ip6_hdr)) {
  1022. n = m_copym(m0, sizeof(struct ip6_hdr),
  1023. hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
  1024. if (n == NULL)
  1025. return (ENOBUFS);
  1026. m->m_next = n;
  1027. } else
  1028. n = m;
  1029. /* Search for the last mbuf of unfragmentable part. */
  1030. for (mlast = n; mlast->m_next; mlast = mlast->m_next)
  1031. ;
  1032. if ((mlast->m_flags & M_EXT) == 0 &&
  1033. M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
  1034. /* use the trailing space of the last mbuf for the fragment hdr */
  1035. *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
  1036. mlast->m_len);
  1037. mlast->m_len += sizeof(struct ip6_frag);
  1038. m->m_pkthdr.len += sizeof(struct ip6_frag);
  1039. } else {
  1040. /* allocate a new mbuf for the fragment header */
  1041. struct mbuf *mfrg;
  1042. MGET(mfrg, M_DONTWAIT, MT_DATA);
  1043. if (mfrg == NULL)
  1044. return (ENOBUFS);
  1045. mfrg->m_len = sizeof(struct ip6_frag);
  1046. *frghdrp = mtod(mfrg, struct ip6_frag *);
  1047. mlast->m_next = mfrg;
  1048. }
  1049. return (0);
  1050. }
  1051. int
  1052. ip6_getpmtu(struct route_in6 *ro_pmtu, struct route_in6 *ro,
  1053. struct ifnet *ifp, struct in6_addr *dst, u_long *mtup, int *alwaysfragp)
  1054. {
  1055. u_int32_t mtu = 0;
  1056. int alwaysfrag = 0;
  1057. int error = 0;
  1058. if (ro_pmtu != ro) {
  1059. /* The first hop and the final destination may differ. */
  1060. struct sockaddr_in6 *sa6_dst = &ro_pmtu->ro_dst;
  1061. if (ro_pmtu->ro_rt &&
  1062. ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 ||
  1063. !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) {
  1064. rtfree(ro_pmtu->ro_rt);
  1065. ro_pmtu->ro_rt = NULL;
  1066. }
  1067. if (ro_pmtu->ro_rt == NULL) {
  1068. bzero(ro_pmtu, sizeof(*ro_pmtu));
  1069. ro_pmtu->ro_tableid = ifp->if_rdomain;
  1070. sa6_dst->sin6_family = AF_INET6;
  1071. sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
  1072. sa6_dst->sin6_addr = *dst;
  1073. ro_pmtu->ro_rt = rtalloc(sin6tosa(&ro_pmtu->ro_dst),
  1074. RT_REPORT|RT_RESOLVE, ro_pmtu->ro_tableid);
  1075. }
  1076. }
  1077. if (ro_pmtu->ro_rt) {
  1078. u_int32_t ifmtu;
  1079. if (ifp == NULL)
  1080. ifp = ro_pmtu->ro_rt->rt_ifp;
  1081. ifmtu = IN6_LINKMTU(ifp);
  1082. mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
  1083. if (mtu == 0)
  1084. mtu = ifmtu;
  1085. else if (mtu < IPV6_MMTU) {
  1086. /*
  1087. * RFC2460 section 5, last paragraph:
  1088. * if we record ICMPv6 too big message with
  1089. * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
  1090. * or smaller, with fragment header attached.
  1091. * (fragment header is needed regardless from the
  1092. * packet size, for translators to identify packets)
  1093. */
  1094. alwaysfrag = 1;
  1095. mtu = IPV6_MMTU;
  1096. } else if (mtu > ifmtu) {
  1097. /*
  1098. * The MTU on the route is larger than the MTU on
  1099. * the interface! This shouldn't happen, unless the
  1100. * MTU of the interface has been changed after the
  1101. * interface was brought up. Change the MTU in the
  1102. * route to match the interface MTU (as long as the
  1103. * field isn't locked).
  1104. */
  1105. mtu = ifmtu;
  1106. if (!(ro_pmtu->ro_rt->rt_rmx.rmx_locks & RTV_MTU))
  1107. ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu;
  1108. }
  1109. } else if (ifp) {
  1110. mtu = IN6_LINKMTU(ifp);
  1111. } else
  1112. error = EHOSTUNREACH; /* XXX */
  1113. *mtup = mtu;
  1114. if (alwaysfragp)
  1115. *alwaysfragp = alwaysfrag;
  1116. return (error);
  1117. }
  1118. /*
  1119. * IP6 socket option processing.
  1120. */
  1121. int
  1122. ip6_ctloutput(int op, struct socket *so, int level, int optname,
  1123. struct mbuf **mp)
  1124. {
  1125. int privileged, optdatalen, uproto;
  1126. void *optdata;
  1127. struct inpcb *inp = sotoinpcb(so);
  1128. struct mbuf *m = *mp;
  1129. int error, optval;
  1130. struct proc *p = curproc; /* For IPSec and rdomain */
  1131. u_int rtid = 0;
  1132. error = optval = 0;
  1133. privileged = (inp->inp_socket->so_state & SS_PRIV);
  1134. uproto = (int)so->so_proto->pr_protocol;
  1135. if (level == IPPROTO_IPV6) {
  1136. switch (op) {
  1137. case PRCO_SETOPT:
  1138. switch (optname) {
  1139. case IPV6_2292PKTOPTIONS:
  1140. error = ip6_pcbopts(&inp->inp_outputopts6,
  1141. m, so);
  1142. break;
  1143. /*
  1144. * Use of some Hop-by-Hop options or some
  1145. * Destination options, might require special
  1146. * privilege. That is, normal applications
  1147. * (without special privilege) might be forbidden
  1148. * from setting certain options in outgoing packets,
  1149. * and might never see certain options in received
  1150. * packets. [RFC 2292 Section 6]
  1151. * KAME specific note:
  1152. * KAME prevents non-privileged users from sending or
  1153. * receiving ANY hbh/dst options in order to avoid
  1154. * overhead of parsing options in the kernel.
  1155. */
  1156. case IPV6_RECVHOPOPTS:
  1157. case IPV6_RECVDSTOPTS:
  1158. case IPV6_RECVRTHDRDSTOPTS:
  1159. if (!privileged) {
  1160. error = EPERM;
  1161. break;
  1162. }
  1163. /* FALLTHROUGH */
  1164. case IPV6_UNICAST_HOPS:
  1165. case IPV6_HOPLIMIT:
  1166. case IPV6_RECVPKTINFO:
  1167. case IPV6_RECVHOPLIMIT:
  1168. case IPV6_RECVRTHDR:
  1169. case IPV6_RECVPATHMTU:
  1170. case IPV6_RECVTCLASS:
  1171. case IPV6_V6ONLY:
  1172. case IPV6_AUTOFLOWLABEL:
  1173. case IPV6_RECVDSTPORT:
  1174. if (m == NULL || m->m_len != sizeof(int)) {
  1175. error = EINVAL;
  1176. break;
  1177. }
  1178. optval = *mtod(m, int *);
  1179. switch (optname) {
  1180. case IPV6_UNICAST_HOPS:
  1181. if (optval < -1 || optval >= 256)
  1182. error = EINVAL;
  1183. else {
  1184. /* -1 = kernel default */
  1185. inp->inp_hops = optval;
  1186. }
  1187. break;
  1188. #define OPTSET(bit) \
  1189. do { \
  1190. if (optval) \
  1191. inp->inp_flags |= (bit); \
  1192. else \
  1193. inp->inp_flags &= ~(bit); \
  1194. } while (/*CONSTCOND*/ 0)
  1195. #define OPTSET2292(bit) \
  1196. do { \
  1197. inp->inp_flags |= IN6P_RFC2292; \
  1198. if (optval) \
  1199. inp->inp_flags |= (bit); \
  1200. else \
  1201. inp->inp_flags &= ~(bit); \
  1202. } while (/*CONSTCOND*/ 0)
  1203. #define OPTBIT(bit) (inp->inp_flags & (bit) ? 1 : 0)
  1204. case IPV6_RECVPKTINFO:
  1205. /* cannot mix with RFC2292 */
  1206. if (OPTBIT(IN6P_RFC2292)) {
  1207. error = EINVAL;
  1208. break;
  1209. }
  1210. OPTSET(IN6P_PKTINFO);
  1211. break;
  1212. case IPV6_HOPLIMIT:
  1213. {
  1214. struct ip6_pktopts **optp;
  1215. /* cannot mix with RFC2292 */
  1216. if (OPTBIT(IN6P_RFC2292)) {
  1217. error = EINVAL;
  1218. break;
  1219. }
  1220. optp = &inp->inp_outputopts6;
  1221. error = ip6_pcbopt(IPV6_HOPLIMIT,
  1222. (u_char *)&optval,
  1223. sizeof(optval),
  1224. optp,
  1225. privileged, uproto);
  1226. break;
  1227. }
  1228. case IPV6_RECVHOPLIMIT:
  1229. /* cannot mix with RFC2292 */
  1230. if (OPTBIT(IN6P_RFC2292)) {
  1231. error = EINVAL;
  1232. break;
  1233. }
  1234. OPTSET(IN6P_HOPLIMIT);
  1235. break;
  1236. case IPV6_RECVHOPOPTS:
  1237. /* cannot mix with RFC2292 */
  1238. if (OPTBIT(IN6P_RFC2292)) {
  1239. error = EINVAL;
  1240. break;
  1241. }
  1242. OPTSET(IN6P_HOPOPTS);
  1243. break;
  1244. case IPV6_RECVDSTOPTS:
  1245. /* cannot mix with RFC2292 */
  1246. if (OPTBIT(IN6P_RFC2292)) {
  1247. error = EINVAL;
  1248. break;
  1249. }
  1250. OPTSET(IN6P_DSTOPTS);
  1251. break;
  1252. case IPV6_RECVRTHDRDSTOPTS:
  1253. /* cannot mix with RFC2292 */
  1254. if (OPTBIT(IN6P_RFC2292)) {
  1255. error = EINVAL;
  1256. break;
  1257. }
  1258. OPTSET(IN6P_RTHDRDSTOPTS);
  1259. break;
  1260. case IPV6_RECVRTHDR:
  1261. /* cannot mix with RFC2292 */
  1262. if (OPTBIT(IN6P_RFC2292)) {
  1263. error = EINVAL;
  1264. break;
  1265. }
  1266. OPTSET(IN6P_RTHDR);
  1267. break;
  1268. case IPV6_RECVPATHMTU:
  1269. /*
  1270. * We ignore this option for TCP
  1271. * sockets.
  1272. * (RFC3542 leaves this case
  1273. * unspecified.)
  1274. */
  1275. if (uproto != IPPROTO_TCP)
  1276. OPTSET(IN6P_MTU);
  1277. break;
  1278. case IPV6_V6ONLY:
  1279. /*
  1280. * make setsockopt(IPV6_V6ONLY)
  1281. * available only prior to bind(2).
  1282. * see ipng mailing list, Jun 22 2001.
  1283. */
  1284. if (inp->inp_lport ||
  1285. !IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6)) {
  1286. error = EINVAL;
  1287. break;
  1288. }
  1289. if ((ip6_v6only && optval) ||
  1290. (!ip6_v6only && !optval))
  1291. error = 0;
  1292. else
  1293. error = EINVAL;
  1294. break;
  1295. case IPV6_RECVTCLASS:
  1296. /* cannot mix with RFC2292 XXX */
  1297. if (OPTBIT(IN6P_RFC2292)) {
  1298. error = EINVAL;
  1299. break;
  1300. }
  1301. OPTSET(IN6P_TCLASS);
  1302. break;
  1303. case IPV6_AUTOFLOWLABEL:
  1304. OPTSET(IN6P_AUTOFLOWLABEL);
  1305. break;
  1306. case IPV6_RECVDSTPORT:
  1307. OPTSET(IN6P_RECVDSTPORT);
  1308. break;
  1309. }
  1310. break;
  1311. case IPV6_TCLASS:
  1312. case IPV6_DONTFRAG:
  1313. case IPV6_USE_MIN_MTU:
  1314. if (m == NULL || m->m_len != sizeof(optval)) {
  1315. error = EINVAL;
  1316. break;
  1317. }
  1318. optval = *mtod(m, int *);
  1319. {
  1320. struct ip6_pktopts **optp;
  1321. optp = &inp->inp_outputopts6;
  1322. error = ip6_pcbopt(optname,
  1323. (u_char *)&optval,
  1324. sizeof(optval),
  1325. optp,
  1326. privileged, uproto);
  1327. break;
  1328. }
  1329. case IPV6_2292PKTINFO:
  1330. case IPV6_2292HOPLIMIT:
  1331. case IPV6_2292HOPOPTS:
  1332. case IPV6_2292DSTOPTS:
  1333. case IPV6_2292RTHDR:
  1334. /* RFC 2292 */
  1335. if (m == NULL || m->m_len != sizeof(int)) {
  1336. error = EINVAL;
  1337. break;
  1338. }
  1339. optval = *mtod(m, int *);
  1340. switch (optname) {
  1341. case IPV6_2292PKTINFO:
  1342. OPTSET2292(IN6P_PKTINFO);
  1343. break;
  1344. case IPV6_2292HOPLIMIT:
  1345. OPTSET2292(IN6P_HOPLIMIT);
  1346. break;
  1347. case IPV6_2292HOPOPTS:
  1348. /*
  1349. * Check super-user privilege.
  1350. * See comments for IPV6_RECVHOPOPTS.
  1351. */
  1352. if (!privileged)
  1353. return (EPERM);
  1354. OPTSET2292(IN6P_HOPOPTS);
  1355. break;
  1356. case IPV6_2292DSTOPTS:
  1357. if (!privileged)
  1358. return (EPERM);
  1359. OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
  1360. break;
  1361. case IPV6_2292RTHDR:
  1362. OPTSET2292(IN6P_RTHDR);
  1363. break;
  1364. }
  1365. break;
  1366. case IPV6_PKTINFO:
  1367. case IPV6_HOPOPTS:
  1368. case IPV6_RTHDR:
  1369. case IPV6_DSTOPTS:
  1370. case IPV6_RTHDRDSTOPTS:
  1371. case IPV6_NEXTHOP:
  1372. {
  1373. /* new advanced API (RFC3542) */
  1374. u_char *optbuf;
  1375. int optbuflen;
  1376. struct ip6_pktopts **optp;
  1377. /* cannot mix with RFC2292 */
  1378. if (OPTBIT(IN6P_RFC2292)) {
  1379. error = EINVAL;
  1380. break;
  1381. }
  1382. if (m && m->m_next) {
  1383. error = EINVAL; /* XXX */
  1384. break;
  1385. }
  1386. if (m) {
  1387. optbuf = mtod(m, u_char *);
  1388. optbuflen = m->m_len;
  1389. } else {
  1390. optbuf = NULL;
  1391. optbuflen = 0;
  1392. }
  1393. optp = &inp->inp_outputopts6;
  1394. error = ip6_pcbopt(optname,
  1395. optbuf, optbuflen,
  1396. optp, privileged, uproto);
  1397. break;
  1398. }
  1399. #undef OPTSET
  1400. case IPV6_MULTICAST_IF:
  1401. case IPV6_MULTICAST_HOPS:
  1402. case IPV6_MULTICAST_LOOP:
  1403. case IPV6_JOIN_GROUP:
  1404. case IPV6_LEAVE_GROUP:
  1405. error = ip6_setmoptions(optname,
  1406. &inp->inp_moptions6,
  1407. m);
  1408. break;
  1409. case IPV6_PORTRANGE:
  1410. if (m == NULL || m->m_len != sizeof(int)) {
  1411. error = EINVAL;
  1412. break;
  1413. }
  1414. optval = *mtod(m, int *);
  1415. switch (optval) {
  1416. case IPV6_PORTRANGE_DEFAULT:
  1417. inp->inp_flags &= ~(IN6P_LOWPORT);
  1418. inp->inp_flags &= ~(IN6P_HIGHPORT);
  1419. break;
  1420. case IPV6_PORTRANGE_HIGH:
  1421. inp->inp_flags &= ~(IN6P_LOWPORT);
  1422. inp->inp_flags |= IN6P_HIGHPORT;
  1423. break;
  1424. case IPV6_PORTRANGE_LOW:
  1425. inp->inp_flags &= ~(IN6P_HIGHPORT);
  1426. inp->inp_flags |= IN6P_LOWPORT;
  1427. break;
  1428. default:
  1429. error = EINVAL;
  1430. break;
  1431. }
  1432. break;
  1433. case IPSEC6_OUTSA:
  1434. error = EINVAL;
  1435. break;
  1436. case IPV6_AUTH_LEVEL:
  1437. case IPV6_ESP_TRANS_LEVEL:
  1438. case IPV6_ESP_NETWORK_LEVEL:
  1439. case IPV6_IPCOMP_LEVEL:
  1440. #ifndef IPSEC
  1441. error = EINVAL;
  1442. #else
  1443. if (m == NULL || m->m_len != sizeof(int)) {
  1444. error = EINVAL;
  1445. break;
  1446. }
  1447. optval = *mtod(m, int *);
  1448. if (optval < IPSEC_LEVEL_BYPASS ||
  1449. optval > IPSEC_LEVEL_UNIQUE) {
  1450. error = EINVAL;
  1451. break;
  1452. }
  1453. switch (optname) {
  1454. case IPV6_AUTH_LEVEL:
  1455. if (optval < IPSEC_AUTH_LEVEL_DEFAULT &&
  1456. suser(p, 0)) {
  1457. error = EACCES;
  1458. break;
  1459. }
  1460. inp->inp_seclevel[SL_AUTH] = optval;
  1461. break;
  1462. case IPV6_ESP_TRANS_LEVEL:
  1463. if (optval < IPSEC_ESP_TRANS_LEVEL_DEFAULT &&
  1464. suser(p, 0)) {
  1465. error = EACCES;
  1466. break;
  1467. }
  1468. inp->inp_seclevel[SL_ESP_TRANS] = optval;
  1469. break;
  1470. case IPV6_ESP_NETWORK_LEVEL:
  1471. if (optval < IPSEC_ESP_NETWORK_LEVEL_DEFAULT &&
  1472. suser(p, 0)) {
  1473. error = EACCES;
  1474. break;
  1475. }
  1476. inp->inp_seclevel[SL_ESP_NETWORK] = optval;
  1477. break;
  1478. case IPV6_IPCOMP_LEVEL:
  1479. if (optval < IPSEC_IPCOMP_LEVEL_DEFAULT &&
  1480. suser(p, 0)) {
  1481. error = EACCES;
  1482. break;
  1483. }
  1484. inp->inp_seclevel[SL_IPCOMP] = optval;
  1485. break;
  1486. }
  1487. #endif
  1488. break;
  1489. case SO_RTABLE:
  1490. if (m == NULL || m->m_len < sizeof(u_int)) {
  1491. error = EINVAL;
  1492. break;
  1493. }
  1494. rtid = *mtod(m, u_int *);
  1495. if (inp->inp_rtableid == rtid)
  1496. break;
  1497. /* needs privileges to switch when already set */
  1498. if (p->p_p->ps_rtableid != rtid &&
  1499. p->p_p->ps_rtableid != 0 &&
  1500. (error = suser(p, 0)) != 0)
  1501. break;
  1502. /* table must exist */
  1503. if (!rtable_exists(rtid)) {
  1504. error = EINVAL;
  1505. break;
  1506. }
  1507. inp->inp_rtableid = rtid;
  1508. break;
  1509. case IPV6_PIPEX:
  1510. if (m != NULL && m->m_len == sizeof(int))
  1511. inp->inp_pipex = *mtod(m, int *);
  1512. else
  1513. error = EINVAL;
  1514. break;
  1515. default:
  1516. error = ENOPROTOOPT;
  1517. break;
  1518. }
  1519. if (m)
  1520. (void)m_free(m);
  1521. break;
  1522. case PRCO_GETOPT:
  1523. switch (optname) {
  1524. case IPV6_2292PKTOPTIONS:
  1525. /*
  1526. * RFC3542 (effectively) deprecated the
  1527. * semantics of the 2292-style pktoptions.
  1528. * Since it was not reliable in nature (i.e.,
  1529. * applications had to expect the lack of some
  1530. * information after all), it would make sense
  1531. * to simplify this part by always returning
  1532. * empty data.
  1533. */
  1534. *mp = m_get(M_WAIT, MT_SOOPTS);
  1535. (*mp)->m_len = 0;
  1536. break;
  1537. case IPV6_RECVHOPOPTS:
  1538. case IPV6_RECVDSTOPTS:
  1539. case IPV6_RECVRTHDRDSTOPTS:
  1540. case IPV6_UNICAST_HOPS:
  1541. case IPV6_RECVPKTINFO:
  1542. case IPV6_RECVHOPLIMIT:
  1543. case IPV6_RECVRTHDR:
  1544. case IPV6_RECVPATHMTU:
  1545. case IPV6_V6ONLY:
  1546. case IPV6_PORTRANGE:
  1547. case IPV6_RECVTCLASS:
  1548. case IPV6_AUTOFLOWLABEL:
  1549. case IPV6_RECVDSTPORT:
  1550. switch (optname) {
  1551. case IPV6_RECVHOPOPTS:
  1552. optval = OPTBIT(IN6P_HOPOPTS);
  1553. break;
  1554. case IPV6_RECVDSTOPTS:
  1555. optval = OPTBIT(IN6P_DSTOPTS);
  1556. break;
  1557. case IPV6_RECVRTHDRDSTOPTS:
  1558. optval = OPTBIT(IN6P_RTHDRDSTOPTS);
  1559. break;
  1560. case IPV6_UNICAST_HOPS:
  1561. optval = inp->inp_hops;
  1562. break;
  1563. case IPV6_RECVPKTINFO:
  1564. optval = OPTBIT(IN6P_PKTINFO);
  1565. break;
  1566. case IPV6_RECVHOPLIMIT:
  1567. optval = OPTBIT(IN6P_HOPLIMIT);
  1568. break;
  1569. case IPV6_RECVRTHDR:
  1570. optval = OPTBIT(IN6P_RTHDR);
  1571. break;
  1572. case IPV6_RECVPATHMTU:
  1573. optval = OPTBIT(IN6P_MTU);
  1574. break;
  1575. case IPV6_V6ONLY:
  1576. optval = (ip6_v6only != 0); /* XXX */
  1577. break;
  1578. case IPV6_PORTRANGE:
  1579. {
  1580. int flags;
  1581. flags = inp->inp_flags;
  1582. if (flags & IN6P_HIGHPORT)
  1583. optval = IPV6_PORTRANGE_HIGH;
  1584. else if (flags & IN6P_LOWPORT)
  1585. optval = IPV6_PORTRANGE_LOW;
  1586. else
  1587. optval = 0;
  1588. break;
  1589. }
  1590. case IPV6_RECVTCLASS:
  1591. optval = OPTBIT(IN6P_TCLASS);
  1592. break;
  1593. case IPV6_AUTOFLOWLABEL:
  1594. optval = OPTBIT(IN6P_AUTOFLOWLABEL);
  1595. break;
  1596. case IPV6_RECVDSTPORT:
  1597. optval = OPTBIT(IN6P_RECVDSTPORT);
  1598. break;
  1599. }
  1600. if (error)
  1601. break;
  1602. *mp = m = m_get(M_WAIT, MT_SOOPTS);
  1603. m->m_len = sizeof(int);
  1604. *mtod(m, int *) = optval;
  1605. break;
  1606. case IPV6_PATHMTU:
  1607. {
  1608. u_long pmtu = 0;
  1609. struct ip6_mtuinfo mtuinfo;
  1610. struct route_in6 *ro = (struct route_in6 *)&inp->inp_route6;
  1611. if (!(so->so_state & SS_ISCONNECTED))
  1612. return (ENOTCONN);
  1613. /*
  1614. * XXX: we dot not consider the case of source
  1615. * routing, or optional information to specify
  1616. * the outgoing interface.
  1617. */
  1618. error = ip6_getpmtu(ro, NULL, NULL,
  1619. &inp->inp_faddr6, &pmtu, NULL);
  1620. if (error)
  1621. break;
  1622. if (pmtu > IPV6_MAXPACKET)
  1623. pmtu = IPV6_MAXPACKET;
  1624. bzero(&mtuinfo, sizeof(mtuinfo));
  1625. mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
  1626. optdata = (void *)&mtuinfo;
  1627. optdatalen = sizeof(mtuinfo);
  1628. if (optdatalen > MCLBYTES)
  1629. return (EMSGSIZE); /* XXX */
  1630. *mp = m = m_get(M_WAIT, MT_SOOPTS);
  1631. if (optdatalen > MLEN)
  1632. MCLGET(m, M_WAIT);
  1633. m->m_len = optdatalen;
  1634. bcopy(optdata, mtod(m, void *), optdatalen);
  1635. break;
  1636. }
  1637. case IPV6_2292PKTINFO:
  1638. case IPV6_2292HOPLIMIT:
  1639. case IPV6_2292HOPOPTS:
  1640. case IPV6_2292RTHDR:
  1641. case IPV6_2292DSTOPTS:
  1642. switch (optname) {
  1643. case IPV6_2292PKTINFO:
  1644. optval = OPTBIT(IN6P_PKTINFO);
  1645. break;
  1646. case IPV6_2292HOPLIMIT:
  1647. optval = OPTBIT(IN6P_HOPLIMIT);
  1648. break;
  1649. case IPV6_2292HOPOPTS:
  1650. optval = OPTBIT(IN6P_HOPOPTS);
  1651. break;
  1652. case IPV6_2292RTHDR:
  1653. optval = OPTBIT(IN6P_RTHDR);
  1654. break;
  1655. case IPV6_2292DSTOPTS:
  1656. optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
  1657. break;
  1658. }
  1659. *mp = m = m_get(M_WAIT, MT_SOOPTS);
  1660. m->m_len = sizeof(int);
  1661. *mtod(m, int *) = optval;
  1662. break;
  1663. case IPV6_PKTINFO:
  1664. case IPV6_HOPOPTS:
  1665. case IPV6_RTHDR:
  1666. case IPV6_DSTOPTS:
  1667. case IPV6_RTHDRDSTOPTS:
  1668. case IPV6_NEXTHOP:
  1669. case IPV6_TCLASS:
  1670. case IPV6_DONTFRAG:
  1671. case IPV6_USE_MIN_MTU:
  1672. error = ip6_getpcbopt(inp->inp_outputopts6,
  1673. optname, mp);
  1674. break;
  1675. case IPV6_MULTICAST_IF:
  1676. case IPV6_MULTICAST_HOPS:
  1677. case IPV6_MULTICAST_LOOP:
  1678. case IPV6_JOIN_GROUP:
  1679. case IPV6_LEAVE_GROUP:
  1680. error = ip6_getmoptions(optname,
  1681. inp->inp_moptions6, mp);
  1682. break;
  1683. case IPSEC6_OUTSA:
  1684. error = EINVAL;
  1685. break;
  1686. case IPV6_AUTH_LEVEL:
  1687. case IPV6_ESP_TRANS_LEVEL:
  1688. case IPV6_ESP_NETWORK_LEVEL:
  1689. case IPV6_IPCOMP_LEVEL:
  1690. *mp = m = m_get(M_WAIT, MT_SOOPTS);
  1691. #ifndef IPSEC
  1692. m->m_len = sizeof(int);
  1693. *mtod(m, int *) = IPSEC_LEVEL_NONE;
  1694. #else
  1695. m->m_len = sizeof(int);
  1696. switch (optname) {
  1697. case IPV6_AUTH_LEVEL:
  1698. optval = inp->inp_seclevel[SL_AUTH];
  1699. break;
  1700. case IPV6_ESP_TRANS_LEVEL:
  1701. optval =
  1702. inp->inp_seclevel[SL_ESP_TRANS];
  1703. break;
  1704. case IPV6_ESP_NETWORK_LEVEL:
  1705. optval =
  1706. inp->inp_seclevel[SL_ESP_NETWORK];
  1707. break;
  1708. case IPV6_IPCOMP_LEVEL:
  1709. optval = inp->inp_seclevel[SL_IPCOMP];
  1710. break;
  1711. }
  1712. *mtod(m, int *) = optval;
  1713. #endif
  1714. break;
  1715. case SO_RTABLE:
  1716. *mp = m = m_get(M_WAIT, MT_SOOPTS);
  1717. m->m_len = sizeof(u_int);
  1718. *mtod(m, u_int *) = optval;
  1719. break;
  1720. case IPV6_PIPEX:
  1721. *mp = m = m_get(M_WAIT, MT_SOOPTS);
  1722. m->m_len = sizeof(int);
  1723. *mtod(m, int *) = optval;
  1724. break;
  1725. default:
  1726. error = ENOPROTOOPT;
  1727. break;
  1728. }
  1729. break;
  1730. }
  1731. } else {
  1732. error = EINVAL;
  1733. if (op == PRCO_SETOPT)
  1734. (void)m_free(*mp);
  1735. }
  1736. return (error);
  1737. }
  1738. int
  1739. ip6_raw_ctloutput(int op, struct socket *so, int level, int optname,
  1740. struct mbuf **mp)
  1741. {
  1742. int error = 0, optval;
  1743. const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
  1744. struct inpcb *inp = sotoinpcb(so);
  1745. struct mbuf *m = *mp;
  1746. if (level != IPPROTO_IPV6) {
  1747. if (op == PRCO_SETOPT)
  1748. (void)m_free(*mp);
  1749. return (EINVAL);
  1750. }
  1751. switch (optname) {
  1752. case IPV6_CHECKSUM:
  1753. /*
  1754. * For ICMPv6 sockets, no modification allowed for checksum
  1755. * offset, permit "no change" values to help existing apps.
  1756. *
  1757. * RFC3542 says: "An attempt to set IPV6_CHECKSUM
  1758. * for an ICMPv6 socket will fail."
  1759. * The current behavior does not meet RFC3542.
  1760. */
  1761. switch (op) {
  1762. case PRCO_SETOPT:
  1763. if (m == NULL || m->m_len != sizeof(int)) {
  1764. error = EINVAL;
  1765. break;
  1766. }
  1767. optval = *mtod(m, int *);
  1768. if ((optval % 2) != 0) {
  1769. /* the API assumes even offset values */
  1770. error = EINVAL;
  1771. } else if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
  1772. if (optval != icmp6off)
  1773. error = EINVAL;
  1774. } else
  1775. inp->inp_cksum6 = optval;
  1776. break;
  1777. case PRCO_GETOPT:
  1778. if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
  1779. optval = icmp6off;
  1780. else
  1781. optval = inp->inp_cksum6;
  1782. *mp = m = m_get(M_WAIT, MT_SOOPTS);
  1783. m->m_len = sizeof(int);
  1784. *mtod(m, int *) = optval;
  1785. break;
  1786. default:
  1787. error = EINVAL;
  1788. break;
  1789. }
  1790. break;
  1791. default:
  1792. error = ENOPROTOOPT;
  1793. break;
  1794. }
  1795. if (op == PRCO_SETOPT)
  1796. (void)m_free(m);
  1797. return (error);
  1798. }
  1799. /*
  1800. * Set up IP6 options in pcb for insertion in output packets.
  1801. * Store in mbuf with pointer in pcbopt, adding pseudo-option
  1802. * with destination address if source routed.
  1803. */
  1804. int
  1805. ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m, struct socket *so)
  1806. {
  1807. struct ip6_pktopts *opt = *pktopt;
  1808. int error = 0;
  1809. struct proc *p = curproc; /* XXX */
  1810. int priv = 0;
  1811. /* turn off any old options. */
  1812. if (opt)
  1813. ip6_clearpktopts(opt, -1);
  1814. else
  1815. opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
  1816. *pktopt = 0;
  1817. if (!m || m->m_len == 0) {
  1818. /*
  1819. * Only turning off any previous options, regardless of
  1820. * whether the opt is just created or given.
  1821. */
  1822. free(opt, M_IP6OPT, 0);
  1823. return (0);
  1824. }
  1825. /* set options specified by user. */
  1826. if (p && !suser(p, 0))
  1827. priv = 1;
  1828. if ((error = ip6_setpktopts(m, opt, NULL, priv,
  1829. so->so_proto->pr_protocol)) != 0) {
  1830. ip6_clearpktopts(opt, -1); /* XXX discard all options */
  1831. free(opt, M_IP6OPT, 0);
  1832. return (error);
  1833. }
  1834. *pktopt = opt;
  1835. return (0);
  1836. }
  1837. /*
  1838. * initialize ip6_pktopts. beware that there are non-zero default values in
  1839. * the struct.
  1840. */
  1841. void
  1842. ip6_initpktopts(struct ip6_pktopts *opt)
  1843. {
  1844. bzero(opt, sizeof(*opt));
  1845. opt->ip6po_hlim = -1; /* -1 means default hop limit */
  1846. opt->ip6po_tclass = -1; /* -1 means default traffic class */
  1847. opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
  1848. }
  1849. int
  1850. ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
  1851. int priv, int uproto)
  1852. {
  1853. struct ip6_pktopts *opt;
  1854. if (*pktopt == NULL) {
  1855. *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
  1856. M_WAITOK);
  1857. ip6_initpktopts(*pktopt);
  1858. }
  1859. opt = *pktopt;
  1860. return (ip6_setpktopt(optname, buf, len, opt, priv, 1, 0, uproto));
  1861. }
  1862. int
  1863. ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct mbuf **mp)
  1864. {
  1865. void *optdata = NULL;
  1866. int optdatalen = 0;
  1867. struct ip6_ext *ip6e;
  1868. int error = 0;
  1869. struct in6_pktinfo null_pktinfo;
  1870. int deftclass = 0, on;
  1871. int defminmtu = IP6PO_MINMTU_MCASTONLY;
  1872. struct mbuf *m;
  1873. switch (optname) {
  1874. case IPV6_PKTINFO:
  1875. if (pktopt && pktopt->ip6po_pktinfo)
  1876. optdata = (void *)pktopt->ip6po_pktinfo;
  1877. else {
  1878. /* XXX: we don't have to do this every time... */
  1879. bzero(&null_pktinfo, sizeof(null_pktinfo));
  1880. optdata = (void *)&null_pktinfo;
  1881. }
  1882. optdatalen = sizeof(struct in6_pktinfo);
  1883. break;
  1884. case IPV6_TCLASS:
  1885. if (pktopt && pktopt->ip6po_tclass >= 0)
  1886. optdata = (void *)&pktopt->ip6po_tclass;
  1887. else
  1888. optdata = (void *)&deftclass;
  1889. optdatalen = sizeof(int);
  1890. break;
  1891. case IPV6_HOPOPTS:
  1892. if (pktopt && pktopt->ip6po_hbh) {
  1893. optdata = (void *)pktopt->ip6po_hbh;
  1894. ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
  1895. optdatalen = (ip6e->ip6e_len + 1) << 3;
  1896. }
  1897. break;
  1898. case IPV6_RTHDR:
  1899. if (pktopt && pktopt->ip6po_rthdr) {
  1900. optdata = (void *)pktopt->ip6po_rthdr;
  1901. ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
  1902. optdatalen = (ip6e->ip6e_len + 1) << 3;
  1903. }
  1904. break;
  1905. case IPV6_RTHDRDSTOPTS:
  1906. if (pktopt && pktopt->ip6po_dest1) {
  1907. optdata = (void *)pktopt->ip6po_dest1;
  1908. ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
  1909. optdatalen = (ip6e->ip6e_len + 1) << 3;
  1910. }
  1911. break;
  1912. case IPV6_DSTOPTS:
  1913. if (pktopt && pktopt->ip6po_dest2) {
  1914. optdata = (void *)pktopt->ip6po_dest2;
  1915. ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
  1916. optdatalen = (ip6e->ip6e_len + 1) << 3;
  1917. }
  1918. break;
  1919. case IPV6_NEXTHOP:
  1920. if (pktopt && pktopt->ip6po_nexthop) {
  1921. optdata = (void *)pktopt->ip6po_nexthop;
  1922. optdatalen = pktopt->ip6po_nexthop->sa_len;
  1923. }
  1924. break;
  1925. case IPV6_USE_MIN_MTU:
  1926. if (pktopt)
  1927. optdata = (void *)&pktopt->ip6po_minmtu;
  1928. else
  1929. optdata = (void *)&defminmtu;
  1930. optdatalen = sizeof(int);
  1931. break;
  1932. case IPV6_DONTFRAG:
  1933. if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
  1934. on = 1;
  1935. else
  1936. on = 0;
  1937. optdata = (void *)&on;
  1938. optdatalen = sizeof(on);
  1939. break;
  1940. default: /* should not happen */
  1941. #ifdef DIAGNOSTIC
  1942. panic("ip6_getpcbopt: unexpected option");
  1943. #endif
  1944. return (ENOPROTOOPT);
  1945. }
  1946. if (optdatalen > MCLBYTES)
  1947. return (EMSGSIZE); /* XXX */
  1948. *mp = m = m_get(M_WAIT, MT_SOOPTS);
  1949. if (optdatalen > MLEN)
  1950. MCLGET(m, M_WAIT);
  1951. m->m_len = optdatalen;
  1952. if (optdatalen)
  1953. bcopy(optdata, mtod(m, void *), optdatalen);
  1954. return (error);
  1955. }
  1956. void
  1957. ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
  1958. {
  1959. if (optname == -1 || optname == IPV6_PKTINFO) {
  1960. if (pktopt->ip6po_pktinfo)
  1961. free(pktopt->ip6po_pktinfo, M_IP6OPT, 0);
  1962. pktopt->ip6po_pktinfo = NULL;
  1963. }
  1964. if (optname == -1 || optname == IPV6_HOPLIMIT)
  1965. pktopt->ip6po_hlim = -1;
  1966. if (optname == -1 || optname == IPV6_TCLASS)
  1967. pktopt->ip6po_tclass = -1;
  1968. if (optname == -1 || optname == IPV6_NEXTHOP) {
  1969. if (pktopt->ip6po_nextroute.ro_rt) {
  1970. rtfree(pktopt->ip6po_nextroute.ro_rt);
  1971. pktopt->ip6po_nextroute.ro_rt = NULL;
  1972. }
  1973. if (pktopt->ip6po_nexthop)
  1974. free(pktopt->ip6po_nexthop, M_IP6OPT, 0);
  1975. pktopt->ip6po_nexthop = NULL;
  1976. }
  1977. if (optname == -1 || optname == IPV6_HOPOPTS) {
  1978. if (pktopt->ip6po_hbh)
  1979. free(pktopt->ip6po_hbh, M_IP6OPT, 0);
  1980. pktopt->ip6po_hbh = NULL;
  1981. }
  1982. if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
  1983. if (pktopt->ip6po_dest1)
  1984. free(pktopt->ip6po_dest1, M_IP6OPT, 0);
  1985. pktopt->ip6po_dest1 = NULL;
  1986. }
  1987. if (optname == -1 || optname == IPV6_RTHDR) {
  1988. if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
  1989. free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT, 0);
  1990. pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
  1991. if (pktopt->ip6po_route.ro_rt) {
  1992. rtfree(pktopt->ip6po_route.ro_rt);
  1993. pktopt->ip6po_route.ro_rt = NULL;
  1994. }
  1995. }
  1996. if (optname == -1 || optname == IPV6_DSTOPTS) {
  1997. if (pktopt->ip6po_dest2)
  1998. free(pktopt->ip6po_dest2, M_IP6OPT, 0);
  1999. pktopt->ip6po_dest2 = NULL;
  2000. }
  2001. }
  2002. #define PKTOPT_EXTHDRCPY(type) \
  2003. do {\
  2004. if (src->type) {\
  2005. int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
  2006. dst->type = malloc(hlen, M_IP6OPT, canwait);\
  2007. if (dst->type == NULL && canwait == M_NOWAIT)\
  2008. goto bad;\
  2009. bcopy(src->type, dst->type, hlen);\
  2010. }\
  2011. } while (/*CONSTCOND*/ 0)
  2012. int
  2013. copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
  2014. {
  2015. dst->ip6po_hlim = src->ip6po_hlim;
  2016. dst->ip6po_tclass = src->ip6po_tclass;
  2017. dst->ip6po_flags = src->ip6po_flags;
  2018. if (src->ip6po_pktinfo) {
  2019. dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
  2020. M_IP6OPT, canwait);
  2021. if (dst->ip6po_pktinfo == NULL)
  2022. goto bad;
  2023. *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
  2024. }
  2025. if (src->ip6po_nexthop) {
  2026. dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
  2027. M_IP6OPT, canwait);
  2028. if (dst->ip6po_nexthop == NULL)
  2029. goto bad;
  2030. bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
  2031. src->ip6po_nexthop->sa_len);
  2032. }
  2033. PKTOPT_EXTHDRCPY(ip6po_hbh);
  2034. PKTOPT_EXTHDRCPY(ip6po_dest1);
  2035. PKTOPT_EXTHDRCPY(ip6po_dest2);
  2036. PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
  2037. return (0);
  2038. bad:
  2039. ip6_clearpktopts(dst, -1);
  2040. return (ENOBUFS);
  2041. }
  2042. #undef PKTOPT_EXTHDRCPY
  2043. void
  2044. ip6_freepcbopts(struct ip6_pktopts *pktopt)
  2045. {
  2046. if (pktopt == NULL)
  2047. return;
  2048. ip6_clearpktopts(pktopt, -1);
  2049. free(pktopt, M_IP6OPT, 0);
  2050. }
  2051. /*
  2052. * Set the IP6 multicast options in response to user setsockopt().
  2053. */
  2054. int
  2055. ip6_setmoptions(int optname, struct ip6_moptions **im6op, struct mbuf *m)
  2056. {
  2057. int error = 0;
  2058. u_int loop, ifindex;
  2059. struct ipv6_mreq *mreq;
  2060. struct ifnet *ifp;
  2061. struct ip6_moptions *im6o = *im6op;
  2062. struct route_in6 ro;
  2063. struct sockaddr_in6 *dst;
  2064. struct in6_multi_mship *imm;
  2065. struct proc *p = curproc; /* XXX */
  2066. if (im6o == NULL) {
  2067. /*
  2068. * No multicast option buffer attached to the pcb;
  2069. * allocate one and initialize to default values.
  2070. */
  2071. im6o = (struct ip6_moptions *)
  2072. malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
  2073. if (im6o == NULL)
  2074. return (ENOBUFS);
  2075. *im6op = im6o;
  2076. im6o->im6o_ifidx = 0;
  2077. im6o->im6o_hlim = ip6_defmcasthlim;
  2078. im6o->im6o_loop = IPV6_DEFAULT_MULTICAST_LOOP;
  2079. LIST_INIT(&im6o->im6o_memberships);
  2080. }
  2081. switch (optname) {
  2082. case IPV6_MULTICAST_IF:
  2083. /*
  2084. * Select the interface for outgoing multicast packets.
  2085. */
  2086. if (m == NULL || m->m_len != sizeof(u_int)) {
  2087. error = EINVAL;
  2088. break;
  2089. }
  2090. bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
  2091. if (ifindex == 0)
  2092. ifp = NULL;
  2093. else {
  2094. ifp = if_get(ifindex);
  2095. if (ifp == NULL) {
  2096. error = ENXIO; /* XXX EINVAL? */
  2097. break;
  2098. }
  2099. if ((ifp->if_flags & IFF_MULTICAST) == 0) {
  2100. error = EADDRNOTAVAIL;
  2101. break;
  2102. }
  2103. }
  2104. im6o->im6o_ifidx = ifindex;
  2105. break;
  2106. case IPV6_MULTICAST_HOPS:
  2107. {
  2108. /*
  2109. * Set the IP6 hoplimit for outgoing multicast packets.
  2110. */
  2111. int optval;
  2112. if (m == NULL || m->m_len != sizeof(int)) {
  2113. error = EINVAL;
  2114. break;
  2115. }
  2116. bcopy(mtod(m, u_int *), &optval, sizeof(optval));
  2117. if (optval < -1 || optval >= 256)
  2118. error = EINVAL;
  2119. else if (optval == -1)
  2120. im6o->im6o_hlim = ip6_defmcasthlim;
  2121. else
  2122. im6o->im6o_hlim = optval;
  2123. break;
  2124. }
  2125. case IPV6_MULTICAST_LOOP:
  2126. /*
  2127. * Set the loopback flag for outgoing multicast packets.
  2128. * Must be zero or one.
  2129. */
  2130. if (m == NULL || m->m_len != sizeof(u_int)) {
  2131. error = EINVAL;
  2132. break;
  2133. }
  2134. bcopy(mtod(m, u_int *), &loop, sizeof(loop));
  2135. if (loop > 1) {
  2136. error = EINVAL;
  2137. break;
  2138. }
  2139. im6o->im6o_loop = loop;
  2140. break;
  2141. case IPV6_JOIN_GROUP:
  2142. /*
  2143. * Add a multicast group membership.
  2144. * Group must be a valid IP6 multicast address.
  2145. */
  2146. if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
  2147. error = EINVAL;
  2148. break;
  2149. }
  2150. mreq = mtod(m, struct ipv6_mreq *);
  2151. if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
  2152. /*
  2153. * We use the unspecified address to specify to accept
  2154. * all multicast addresses. Only super user is allowed
  2155. * to do this.
  2156. */
  2157. if (suser(p, 0))
  2158. {
  2159. error = EACCES;
  2160. break;
  2161. }
  2162. } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
  2163. error = EINVAL;
  2164. break;
  2165. }
  2166. /*
  2167. * If no interface was explicitly specified, choose an
  2168. * appropriate one according to the given multicast address.
  2169. */
  2170. if (mreq->ipv6mr_interface == 0) {
  2171. /*
  2172. * Look up the routing table for the
  2173. * address, and choose the outgoing interface.
  2174. * XXX: is it a good approach?
  2175. */
  2176. bzero(&ro, sizeof(ro));
  2177. ro.ro_tableid = m->m_pkthdr.ph_rtableid;
  2178. dst = &ro.ro_dst;
  2179. dst->sin6_len = sizeof(struct sockaddr_in6);
  2180. dst->sin6_family = AF_INET6;
  2181. dst->sin6_addr = mreq->ipv6mr_multiaddr;
  2182. ro.ro_rt = rtalloc(sin6tosa(&ro.ro_dst),
  2183. RT_REPORT|RT_RESOLVE, ro.ro_tableid);
  2184. if (ro.ro_rt == NULL) {
  2185. error = EADDRNOTAVAIL;
  2186. break;
  2187. }
  2188. ifp = ro.ro_rt->rt_ifp;
  2189. rtfree(ro.ro_rt);
  2190. } else {
  2191. /*
  2192. * If the interface is specified, validate it.
  2193. */
  2194. ifp = if_get(mreq->ipv6mr_interface);
  2195. if (ifp == NULL) {
  2196. error = ENXIO; /* XXX EINVAL? */
  2197. break;
  2198. }
  2199. }
  2200. /*
  2201. * See if we found an interface, and confirm that it
  2202. * supports multicast
  2203. */
  2204. if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
  2205. error = EADDRNOTAVAIL;
  2206. break;
  2207. }
  2208. /*
  2209. * Put interface index into the multicast address,
  2210. * if the address has link/interface-local scope.
  2211. */
  2212. if (IN6_IS_SCOPE_EMBED(&mreq->ipv6mr_multiaddr)) {
  2213. mreq->ipv6mr_multiaddr.s6_addr16[1] =
  2214. htons(ifp->if_index);
  2215. }
  2216. /*
  2217. * See if the membership already exists.
  2218. */
  2219. LIST_FOREACH(imm, &im6o->im6o_memberships, i6mm_chain)
  2220. if (imm->i6mm_maddr->in6m_ifidx == ifp->if_index &&
  2221. IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
  2222. &mreq->ipv6mr_multiaddr))
  2223. break;
  2224. if (imm != NULL) {
  2225. error = EADDRINUSE;
  2226. break;
  2227. }
  2228. /*
  2229. * Everything looks good; add a new record to the multicast
  2230. * address list for the given interface.
  2231. */
  2232. imm = in6_joingroup(ifp, &mreq->ipv6mr_multiaddr, &error);
  2233. if (!imm)
  2234. break;
  2235. LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
  2236. break;
  2237. case IPV6_LEAVE_GROUP:
  2238. /*
  2239. * Drop a multicast group membership.
  2240. * Group must be a valid IP6 multicast address.
  2241. */
  2242. if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
  2243. error = EINVAL;
  2244. break;
  2245. }
  2246. mreq = mtod(m, struct ipv6_mreq *);
  2247. if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
  2248. if (suser(p, 0))
  2249. {
  2250. error = EACCES;
  2251. break;
  2252. }
  2253. } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
  2254. error = EINVAL;
  2255. break;
  2256. }
  2257. /*
  2258. * If an interface address was specified, get a pointer
  2259. * to its ifnet structure.
  2260. */
  2261. if (mreq->ipv6mr_interface == 0)
  2262. ifp = NULL;
  2263. else {
  2264. ifp = if_get(mreq->ipv6mr_interface);
  2265. if (ifp == NULL) {
  2266. error = ENXIO; /* XXX EINVAL? */
  2267. break;
  2268. }
  2269. }
  2270. /*
  2271. * Put interface index into the multicast address,
  2272. * if the address has link-local scope.
  2273. */
  2274. if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
  2275. mreq->ipv6mr_multiaddr.s6_addr16[1] =
  2276. htons(mreq->ipv6mr_interface);
  2277. }
  2278. /*
  2279. * Find the membership in the membership list.
  2280. */
  2281. LIST_FOREACH(imm, &im6o->im6o_memberships, i6mm_chain) {
  2282. if ((ifp == NULL ||
  2283. imm->i6mm_maddr->in6m_ifidx == ifp->if_index) &&
  2284. IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
  2285. &mreq->ipv6mr_multiaddr))
  2286. break;
  2287. }
  2288. if (imm == NULL) {
  2289. /* Unable to resolve interface */
  2290. error = EADDRNOTAVAIL;
  2291. break;
  2292. }
  2293. /*
  2294. * Give up the multicast address record to which the
  2295. * membership points.
  2296. */
  2297. LIST_REMOVE(imm, i6mm_chain);
  2298. in6_leavegroup(imm);
  2299. break;
  2300. default:
  2301. error = EOPNOTSUPP;
  2302. break;
  2303. }
  2304. /*
  2305. * If all options have default values, no need to keep the option
  2306. * structure.
  2307. */
  2308. if (im6o->im6o_ifidx == 0 &&
  2309. im6o->im6o_hlim == ip6_defmcasthlim &&
  2310. im6o->im6o_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
  2311. LIST_EMPTY(&im6o->im6o_memberships)) {
  2312. free(*im6op, M_IPMOPTS, 0);
  2313. *im6op = NULL;
  2314. }
  2315. return (error);
  2316. }
  2317. /*
  2318. * Return the IP6 multicast options in response to user getsockopt().
  2319. */
  2320. int
  2321. ip6_getmoptions(int optname, struct ip6_moptions *im6o, struct mbuf **mp)
  2322. {
  2323. u_int *hlim, *loop, *ifindex;
  2324. *mp = m_get(M_WAIT, MT_SOOPTS);
  2325. switch (optname) {
  2326. case IPV6_MULTICAST_IF:
  2327. ifindex = mtod(*mp, u_int *);
  2328. (*mp)->m_len = sizeof(u_int);
  2329. if (im6o == NULL || im6o->im6o_ifidx == 0)
  2330. *ifindex = 0;
  2331. else
  2332. *ifindex = im6o->im6o_ifidx;
  2333. return (0);
  2334. case IPV6_MULTICAST_HOPS:
  2335. hlim = mtod(*mp, u_int *);
  2336. (*mp)->m_len = sizeof(u_int);
  2337. if (im6o == NULL)
  2338. *hlim = ip6_defmcasthlim;
  2339. else
  2340. *hlim = im6o->im6o_hlim;
  2341. return (0);
  2342. case IPV6_MULTICAST_LOOP:
  2343. loop = mtod(*mp, u_int *);
  2344. (*mp)->m_len = sizeof(u_int);
  2345. if (im6o == NULL)
  2346. *loop = ip6_defmcasthlim;
  2347. else
  2348. *loop = im6o->im6o_loop;
  2349. return (0);
  2350. default:
  2351. return (EOPNOTSUPP);
  2352. }
  2353. }
  2354. /*
  2355. * Discard the IP6 multicast options.
  2356. */
  2357. void
  2358. ip6_freemoptions(struct ip6_moptions *im6o)
  2359. {
  2360. struct in6_multi_mship *imm;
  2361. if (im6o == NULL)
  2362. return;
  2363. while (!LIST_EMPTY(&im6o->im6o_memberships)) {
  2364. imm = LIST_FIRST(&im6o->im6o_memberships);
  2365. LIST_REMOVE(imm, i6mm_chain);
  2366. in6_leavegroup(imm);
  2367. }
  2368. free(im6o, M_IPMOPTS, 0);
  2369. }
  2370. /*
  2371. * Set IPv6 outgoing packet options based on advanced API.
  2372. */
  2373. int
  2374. ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
  2375. struct ip6_pktopts *stickyopt, int priv, int uproto)
  2376. {
  2377. u_int clen;
  2378. struct cmsghdr *cm = 0;
  2379. caddr_t cmsgs;
  2380. int error;
  2381. if (control == NULL || opt == NULL)
  2382. return (EINVAL);
  2383. ip6_initpktopts(opt);
  2384. if (stickyopt) {
  2385. int error;
  2386. /*
  2387. * If stickyopt is provided, make a local copy of the options
  2388. * for this particular packet, then override them by ancillary
  2389. * objects.
  2390. * XXX: copypktopts() does not copy the cached route to a next
  2391. * hop (if any). This is not very good in terms of efficiency,
  2392. * but we can allow this since this option should be rarely
  2393. * used.
  2394. */
  2395. if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
  2396. return (error);
  2397. }
  2398. /*
  2399. * XXX: Currently, we assume all the optional information is stored
  2400. * in a single mbuf.
  2401. */
  2402. if (control->m_next)
  2403. return (EINVAL);
  2404. clen = control->m_len;
  2405. cmsgs = mtod(control, caddr_t);
  2406. do {
  2407. if (clen < CMSG_LEN(0))
  2408. return (EINVAL);
  2409. cm = (struct cmsghdr *)cmsgs;
  2410. if (cm->cmsg_len < CMSG_LEN(0) || cm->cmsg_len > clen ||
  2411. CMSG_ALIGN(cm->cmsg_len) > clen)
  2412. return (EINVAL);
  2413. if (cm->cmsg_level == IPPROTO_IPV6) {
  2414. error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
  2415. cm->cmsg_len - CMSG_LEN(0), opt, priv, 0, 1, uproto);
  2416. if (error)
  2417. return (error);
  2418. }
  2419. clen -= CMSG_ALIGN(cm->cmsg_len);
  2420. cmsgs += CMSG_ALIGN(cm->cmsg_len);
  2421. } while (clen);
  2422. return (0);
  2423. }
  2424. /*
  2425. * Set a particular packet option, as a sticky option or an ancillary data
  2426. * item. "len" can be 0 only when it's a sticky option.
  2427. * We have 4 cases of combination of "sticky" and "cmsg":
  2428. * "sticky=0, cmsg=0": impossible
  2429. * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
  2430. * "sticky=1, cmsg=0": RFC3542 socket option
  2431. * "sticky=1, cmsg=1": RFC2292 socket option
  2432. */
  2433. int
  2434. ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
  2435. int priv, int sticky, int cmsg, int uproto)
  2436. {
  2437. int minmtupolicy;
  2438. if (!sticky && !cmsg) {
  2439. #ifdef DIAGNOSTIC
  2440. printf("ip6_setpktopt: impossible case\n");
  2441. #endif
  2442. return (EINVAL);
  2443. }
  2444. /*
  2445. * IPV6_2292xxx is for backward compatibility to RFC2292, and should
  2446. * not be specified in the context of RFC3542. Conversely,
  2447. * RFC3542 types should not be specified in the context of RFC2292.
  2448. */
  2449. if (!cmsg) {
  2450. switch (optname) {
  2451. case IPV6_2292PKTINFO:
  2452. case IPV6_2292HOPLIMIT:
  2453. case IPV6_2292NEXTHOP:
  2454. case IPV6_2292HOPOPTS:
  2455. case IPV6_2292DSTOPTS:
  2456. case IPV6_2292RTHDR:
  2457. case IPV6_2292PKTOPTIONS:
  2458. return (ENOPROTOOPT);
  2459. }
  2460. }
  2461. if (sticky && cmsg) {
  2462. switch (optname) {
  2463. case IPV6_PKTINFO:
  2464. case IPV6_HOPLIMIT:
  2465. case IPV6_NEXTHOP:
  2466. case IPV6_HOPOPTS:
  2467. case IPV6_DSTOPTS:
  2468. case IPV6_RTHDRDSTOPTS:
  2469. case IPV6_RTHDR:
  2470. case IPV6_USE_MIN_MTU:
  2471. case IPV6_DONTFRAG:
  2472. case IPV6_TCLASS:
  2473. return (ENOPROTOOPT);
  2474. }
  2475. }
  2476. switch (optname) {
  2477. case IPV6_2292PKTINFO:
  2478. case IPV6_PKTINFO:
  2479. {
  2480. struct ifnet *ifp = NULL;
  2481. struct in6_pktinfo *pktinfo;
  2482. if (len != sizeof(struct in6_pktinfo))
  2483. return (EINVAL);
  2484. pktinfo = (struct in6_pktinfo *)buf;
  2485. /*
  2486. * An application can clear any sticky IPV6_PKTINFO option by
  2487. * doing a "regular" setsockopt with ipi6_addr being
  2488. * in6addr_any and ipi6_ifindex being zero.
  2489. * [RFC 3542, Section 6]
  2490. */
  2491. if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
  2492. pktinfo->ipi6_ifindex == 0 &&
  2493. IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
  2494. ip6_clearpktopts(opt, optname);
  2495. break;
  2496. }
  2497. if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
  2498. sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
  2499. return (EINVAL);
  2500. }
  2501. if (pktinfo->ipi6_ifindex) {
  2502. ifp = if_get(pktinfo->ipi6_ifindex);
  2503. if (ifp == NULL)
  2504. return (ENXIO);
  2505. }
  2506. /*
  2507. * We store the address anyway, and let in6_selectsrc()
  2508. * validate the specified address. This is because ipi6_addr
  2509. * may not have enough information about its scope zone, and
  2510. * we may need additional information (such as outgoing
  2511. * interface or the scope zone of a destination address) to
  2512. * disambiguate the scope.
  2513. * XXX: the delay of the validation may confuse the
  2514. * application when it is used as a sticky option.
  2515. */
  2516. if (opt->ip6po_pktinfo == NULL) {
  2517. opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
  2518. M_IP6OPT, M_NOWAIT);
  2519. if (opt->ip6po_pktinfo == NULL)
  2520. return (ENOBUFS);
  2521. }
  2522. bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
  2523. break;
  2524. }
  2525. case IPV6_2292HOPLIMIT:
  2526. case IPV6_HOPLIMIT:
  2527. {
  2528. int *hlimp;
  2529. /*
  2530. * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
  2531. * to simplify the ordering among hoplimit options.
  2532. */
  2533. if (optname == IPV6_HOPLIMIT && sticky)
  2534. return (ENOPROTOOPT);
  2535. if (len != sizeof(int))
  2536. return (EINVAL);
  2537. hlimp = (int *)buf;
  2538. if (*hlimp < -1 || *hlimp > 255)
  2539. return (EINVAL);
  2540. opt->ip6po_hlim = *hlimp;
  2541. break;
  2542. }
  2543. case IPV6_TCLASS:
  2544. {
  2545. int tclass;
  2546. if (len != sizeof(int))
  2547. return (EINVAL);
  2548. tclass = *(int *)buf;
  2549. if (tclass < -1 || tclass > 255)
  2550. return (EINVAL);
  2551. opt->ip6po_tclass = tclass;
  2552. break;
  2553. }
  2554. case IPV6_2292NEXTHOP:
  2555. case IPV6_NEXTHOP:
  2556. if (!priv)
  2557. return (EPERM);
  2558. if (len == 0) { /* just remove the option */
  2559. ip6_clearpktopts(opt, IPV6_NEXTHOP);
  2560. break;
  2561. }
  2562. /* check if cmsg_len is large enough for sa_len */
  2563. if (len < sizeof(struct sockaddr) || len < *buf)
  2564. return (EINVAL);
  2565. switch (((struct sockaddr *)buf)->sa_family) {
  2566. case AF_INET6:
  2567. {
  2568. struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
  2569. if (sa6->sin6_len != sizeof(struct sockaddr_in6))
  2570. return (EINVAL);
  2571. if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
  2572. IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
  2573. return (EINVAL);
  2574. }
  2575. if (IN6_IS_SCOPE_EMBED(&sa6->sin6_addr)) {
  2576. if (if_get(sa6->sin6_scope_id) == NULL)
  2577. return (EINVAL);
  2578. sa6->sin6_addr.s6_addr16[1] =
  2579. htonl(sa6->sin6_scope_id);
  2580. } else if (sa6->sin6_scope_id)
  2581. return (EINVAL);
  2582. break;
  2583. }
  2584. case AF_LINK: /* eventually be supported? */
  2585. default:
  2586. return (EAFNOSUPPORT);
  2587. }
  2588. /* turn off the previous option, then set the new option. */
  2589. ip6_clearpktopts(opt, IPV6_NEXTHOP);
  2590. opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
  2591. if (opt->ip6po_nexthop == NULL)
  2592. return (ENOBUFS);
  2593. bcopy(buf, opt->ip6po_nexthop, *buf);
  2594. break;
  2595. case IPV6_2292HOPOPTS:
  2596. case IPV6_HOPOPTS:
  2597. {
  2598. struct ip6_hbh *hbh;
  2599. int hbhlen;
  2600. /*
  2601. * XXX: We don't allow a non-privileged user to set ANY HbH
  2602. * options, since per-option restriction has too much
  2603. * overhead.
  2604. */
  2605. if (!priv)
  2606. return (EPERM);
  2607. if (len == 0) {
  2608. ip6_clearpktopts(opt, IPV6_HOPOPTS);
  2609. break; /* just remove the option */
  2610. }
  2611. /* message length validation */
  2612. if (len < sizeof(struct ip6_hbh))
  2613. return (EINVAL);
  2614. hbh = (struct ip6_hbh *)buf;
  2615. hbhlen = (hbh->ip6h_len + 1) << 3;
  2616. if (len != hbhlen)
  2617. return (EINVAL);
  2618. /* turn off the previous option, then set the new option. */
  2619. ip6_clearpktopts(opt, IPV6_HOPOPTS);
  2620. opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
  2621. if (opt->ip6po_hbh == NULL)
  2622. return (ENOBUFS);
  2623. bcopy(hbh, opt->ip6po_hbh, hbhlen);
  2624. break;
  2625. }
  2626. case IPV6_2292DSTOPTS:
  2627. case IPV6_DSTOPTS:
  2628. case IPV6_RTHDRDSTOPTS:
  2629. {
  2630. struct ip6_dest *dest, **newdest = NULL;
  2631. int destlen;
  2632. if (!priv) /* XXX: see the comment for IPV6_HOPOPTS */
  2633. return (EPERM);
  2634. if (len == 0) {
  2635. ip6_clearpktopts(opt, optname);
  2636. break; /* just remove the option */
  2637. }
  2638. /* message length validation */
  2639. if (len < sizeof(struct ip6_dest))
  2640. return (EINVAL);
  2641. dest = (struct ip6_dest *)buf;
  2642. destlen = (dest->ip6d_len + 1) << 3;
  2643. if (len != destlen)
  2644. return (EINVAL);
  2645. /*
  2646. * Determine the position that the destination options header
  2647. * should be inserted; before or after the routing header.
  2648. */
  2649. switch (optname) {
  2650. case IPV6_2292DSTOPTS:
  2651. /*
  2652. * The old advanced API is ambiguous on this point.
  2653. * Our approach is to determine the position based
  2654. * according to the existence of a routing header.
  2655. * Note, however, that this depends on the order of the
  2656. * extension headers in the ancillary data; the 1st
  2657. * part of the destination options header must appear
  2658. * before the routing header in the ancillary data,
  2659. * too.
  2660. * RFC3542 solved the ambiguity by introducing
  2661. * separate ancillary data or option types.
  2662. */
  2663. if (opt->ip6po_rthdr == NULL)
  2664. newdest = &opt->ip6po_dest1;
  2665. else
  2666. newdest = &opt->ip6po_dest2;
  2667. break;
  2668. case IPV6_RTHDRDSTOPTS:
  2669. newdest = &opt->ip6po_dest1;
  2670. break;
  2671. case IPV6_DSTOPTS:
  2672. newdest = &opt->ip6po_dest2;
  2673. break;
  2674. }
  2675. /* turn off the previous option, then set the new option. */
  2676. ip6_clearpktopts(opt, optname);
  2677. *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
  2678. if (*newdest == NULL)
  2679. return (ENOBUFS);
  2680. bcopy(dest, *newdest, destlen);
  2681. break;
  2682. }
  2683. case IPV6_2292RTHDR:
  2684. case IPV6_RTHDR:
  2685. {
  2686. struct ip6_rthdr *rth;
  2687. int rthlen;
  2688. if (len == 0) {
  2689. ip6_clearpktopts(opt, IPV6_RTHDR);
  2690. break; /* just remove the option */
  2691. }
  2692. /* message length validation */
  2693. if (len < sizeof(struct ip6_rthdr))
  2694. return (EINVAL);
  2695. rth = (struct ip6_rthdr *)buf;
  2696. rthlen = (rth->ip6r_len + 1) << 3;
  2697. if (len != rthlen)
  2698. return (EINVAL);
  2699. switch (rth->ip6r_type) {
  2700. case IPV6_RTHDR_TYPE_0:
  2701. if (rth->ip6r_len == 0) /* must contain one addr */
  2702. return (EINVAL);
  2703. if (rth->ip6r_len % 2) /* length must be even */
  2704. return (EINVAL);
  2705. if (rth->ip6r_len / 2 != rth->ip6r_segleft)
  2706. return (EINVAL);
  2707. break;
  2708. default:
  2709. return (EINVAL); /* not supported */
  2710. }
  2711. /* turn off the previous option */
  2712. ip6_clearpktopts(opt, IPV6_RTHDR);
  2713. opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
  2714. if (opt->ip6po_rthdr == NULL)
  2715. return (ENOBUFS);
  2716. bcopy(rth, opt->ip6po_rthdr, rthlen);
  2717. break;
  2718. }
  2719. case IPV6_USE_MIN_MTU:
  2720. if (len != sizeof(int))
  2721. return (EINVAL);
  2722. minmtupolicy = *(int *)buf;
  2723. if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
  2724. minmtupolicy != IP6PO_MINMTU_DISABLE &&
  2725. minmtupolicy != IP6PO_MINMTU_ALL) {
  2726. return (EINVAL);
  2727. }
  2728. opt->ip6po_minmtu = minmtupolicy;
  2729. break;
  2730. case IPV6_DONTFRAG:
  2731. if (len != sizeof(int))
  2732. return (EINVAL);
  2733. if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
  2734. /*
  2735. * we ignore this option for TCP sockets.
  2736. * (RFC3542 leaves this case unspecified.)
  2737. */
  2738. opt->ip6po_flags &= ~IP6PO_DONTFRAG;
  2739. } else
  2740. opt->ip6po_flags |= IP6PO_DONTFRAG;
  2741. break;
  2742. default:
  2743. return (ENOPROTOOPT);
  2744. } /* end of switch */
  2745. return (0);
  2746. }
  2747. /*
  2748. * Routine called from ip6_output() to loop back a copy of an IP6 multicast
  2749. * packet to the input queue of a specified interface. Note that this
  2750. * calls the output routine of the loopback "driver", but with an interface
  2751. * pointer that might NOT be lo0ifp -- easier than replicating that code here.
  2752. */
  2753. void
  2754. ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst)
  2755. {
  2756. struct mbuf *copym;
  2757. struct ip6_hdr *ip6;
  2758. /*
  2759. * Duplicate the packet.
  2760. */
  2761. copym = m_copym(m, 0, M_COPYALL, M_NOWAIT);
  2762. if (copym == NULL)
  2763. return;
  2764. /*
  2765. * Make sure to deep-copy IPv6 header portion in case the data
  2766. * is in an mbuf cluster, so that we can safely override the IPv6
  2767. * header portion later.
  2768. */
  2769. if ((copym->m_flags & M_EXT) != 0 ||
  2770. copym->m_len < sizeof(struct ip6_hdr)) {
  2771. copym = m_pullup(copym, sizeof(struct ip6_hdr));
  2772. if (copym == NULL)
  2773. return;
  2774. }
  2775. #ifdef DIAGNOSTIC
  2776. if (copym->m_len < sizeof(*ip6)) {
  2777. m_freem(copym);
  2778. return;
  2779. }
  2780. #endif
  2781. ip6 = mtod(copym, struct ip6_hdr *);
  2782. if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src))
  2783. ip6->ip6_src.s6_addr16[1] = 0;
  2784. if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst))
  2785. ip6->ip6_dst.s6_addr16[1] = 0;
  2786. (void)looutput(ifp, copym, sin6tosa(dst), NULL);
  2787. }
  2788. /*
  2789. * Chop IPv6 header off from the payload.
  2790. */
  2791. int
  2792. ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
  2793. {
  2794. struct mbuf *mh;
  2795. struct ip6_hdr *ip6;
  2796. ip6 = mtod(m, struct ip6_hdr *);
  2797. if (m->m_len > sizeof(*ip6)) {
  2798. MGETHDR(mh, M_DONTWAIT, MT_HEADER);
  2799. if (mh == NULL) {
  2800. m_freem(m);
  2801. return ENOBUFS;
  2802. }
  2803. M_MOVE_PKTHDR(mh, m);
  2804. MH_ALIGN(mh, sizeof(*ip6));
  2805. m->m_len -= sizeof(*ip6);
  2806. m->m_data += sizeof(*ip6);
  2807. mh->m_next = m;
  2808. m = mh;
  2809. m->m_len = sizeof(*ip6);
  2810. bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
  2811. }
  2812. exthdrs->ip6e_ip6 = m;
  2813. return 0;
  2814. }
  2815. u_int32_t
  2816. ip6_randomid(void)
  2817. {
  2818. return idgen32(&ip6_id_ctx);
  2819. }
  2820. void
  2821. ip6_randomid_init(void)
  2822. {
  2823. idgen32_init(&ip6_id_ctx);
  2824. }
  2825. /*
  2826. * Compute significant parts of the IPv6 checksum pseudo-header
  2827. * for use in a delayed TCP/UDP checksum calculation.
  2828. */
  2829. static __inline u_int16_t __attribute__((__unused__))
  2830. in6_cksum_phdr(const struct in6_addr *src, const struct in6_addr *dst,
  2831. u_int32_t len, u_int32_t nxt)
  2832. {
  2833. u_int32_t sum = 0;
  2834. const u_int16_t *w;
  2835. w = (const u_int16_t *) src;
  2836. sum += w[0];
  2837. if (!IN6_IS_SCOPE_EMBED(src))
  2838. sum += w[1];
  2839. sum += w[2]; sum += w[3]; sum += w[4]; sum += w[5];
  2840. sum += w[6]; sum += w[7];
  2841. w = (const u_int16_t *) dst;
  2842. sum += w[0];
  2843. if (!IN6_IS_SCOPE_EMBED(dst))
  2844. sum += w[1];
  2845. sum += w[2]; sum += w[3]; sum += w[4]; sum += w[5];
  2846. sum += w[6]; sum += w[7];
  2847. sum += (u_int16_t)(len >> 16) + (u_int16_t)(len /*& 0xffff*/);
  2848. sum += (u_int16_t)(nxt >> 16) + (u_int16_t)(nxt /*& 0xffff*/);
  2849. sum = (u_int16_t)(sum >> 16) + (u_int16_t)(sum /*& 0xffff*/);
  2850. if (sum > 0xffff)
  2851. sum -= 0xffff;
  2852. return (sum);
  2853. }
  2854. /*
  2855. * Process a delayed payload checksum calculation.
  2856. */
  2857. void
  2858. in6_delayed_cksum(struct mbuf *m, u_int8_t nxt)
  2859. {
  2860. int nxtp, offset;
  2861. u_int16_t csum;
  2862. offset = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxtp);
  2863. if (offset <= 0 || nxtp != nxt)
  2864. /* If the desired next protocol isn't found, punt. */
  2865. return;
  2866. csum = (u_int16_t)(in6_cksum(m, 0, offset, m->m_pkthdr.len - offset));
  2867. switch (nxt) {
  2868. case IPPROTO_TCP:
  2869. offset += offsetof(struct tcphdr, th_sum);
  2870. break;
  2871. case IPPROTO_UDP:
  2872. offset += offsetof(struct udphdr, uh_sum);
  2873. if (csum == 0)
  2874. csum = 0xffff;
  2875. break;
  2876. case IPPROTO_ICMPV6:
  2877. offset += offsetof(struct icmp6_hdr, icmp6_cksum);
  2878. break;
  2879. }
  2880. if ((offset + sizeof(u_int16_t)) > m->m_len)
  2881. m_copyback(m, offset, sizeof(csum), &csum, M_NOWAIT);
  2882. else
  2883. *(u_int16_t *)(mtod(m, caddr_t) + offset) = csum;
  2884. }
  2885. void
  2886. in6_proto_cksum_out(struct mbuf *m, struct ifnet *ifp)
  2887. {
  2888. /* some hw and in6_delayed_cksum need the pseudo header cksum */
  2889. if (m->m_pkthdr.csum_flags &
  2890. (M_TCP_CSUM_OUT|M_UDP_CSUM_OUT|M_ICMP_CSUM_OUT)) {
  2891. struct ip6_hdr *ip6;
  2892. int nxt, offset;
  2893. u_int16_t csum;
  2894. ip6 = mtod(m, struct ip6_hdr *);
  2895. offset = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
  2896. csum = in6_cksum_phdr(&ip6->ip6_src, &ip6->ip6_dst,
  2897. htonl(m->m_pkthdr.len - offset), htonl(nxt));
  2898. if (nxt == IPPROTO_TCP)
  2899. offset += offsetof(struct tcphdr, th_sum);
  2900. else if (nxt == IPPROTO_UDP)
  2901. offset += offsetof(struct udphdr, uh_sum);
  2902. else if (nxt == IPPROTO_ICMPV6)
  2903. offset += offsetof(struct icmp6_hdr, icmp6_cksum);
  2904. if ((offset + sizeof(u_int16_t)) > m->m_len)
  2905. m_copyback(m, offset, sizeof(csum), &csum, M_NOWAIT);
  2906. else
  2907. *(u_int16_t *)(mtod(m, caddr_t) + offset) = csum;
  2908. }
  2909. if (m->m_pkthdr.csum_flags & M_TCP_CSUM_OUT) {
  2910. if (!ifp || !(ifp->if_capabilities & IFCAP_CSUM_TCPv6) ||
  2911. ifp->if_bridgeport != NULL) {
  2912. tcpstat.tcps_outswcsum++;
  2913. in6_delayed_cksum(m, IPPROTO_TCP);
  2914. m->m_pkthdr.csum_flags &= ~M_TCP_CSUM_OUT; /* Clear */
  2915. }
  2916. } else if (m->m_pkthdr.csum_flags & M_UDP_CSUM_OUT) {
  2917. if (!ifp || !(ifp->if_capabilities & IFCAP_CSUM_UDPv6) ||
  2918. ifp->if_bridgeport != NULL) {
  2919. udpstat.udps_outswcsum++;
  2920. in6_delayed_cksum(m, IPPROTO_UDP);
  2921. m->m_pkthdr.csum_flags &= ~M_UDP_CSUM_OUT; /* Clear */
  2922. }
  2923. } else if (m->m_pkthdr.csum_flags & M_ICMP_CSUM_OUT) {
  2924. in6_delayed_cksum(m, IPPROTO_ICMPV6);
  2925. m->m_pkthdr.csum_flags &= ~M_ICMP_CSUM_OUT; /* Clear */
  2926. }
  2927. }