ipv6.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* SCTP kernel implementation
  3. * (C) Copyright IBM Corp. 2002, 2004
  4. * Copyright (c) 2001 Nokia, Inc.
  5. * Copyright (c) 2001 La Monte H.P. Yarroll
  6. * Copyright (c) 2002-2003 Intel Corp.
  7. *
  8. * This file is part of the SCTP kernel implementation
  9. *
  10. * SCTP over IPv6.
  11. *
  12. * Please send any bug reports or fixes you make to the
  13. * email address(es):
  14. * lksctp developers <linux-sctp@vger.kernel.org>
  15. *
  16. * Written or modified by:
  17. * Le Yanqun <yanqun.le@nokia.com>
  18. * Hui Huang <hui.huang@nokia.com>
  19. * La Monte H.P. Yarroll <piggy@acm.org>
  20. * Sridhar Samudrala <sri@us.ibm.com>
  21. * Jon Grimm <jgrimm@us.ibm.com>
  22. * Ardelle Fan <ardelle.fan@intel.com>
  23. *
  24. * Based on:
  25. * linux/net/ipv6/tcp_ipv6.c
  26. */
  27. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  28. #include <linux/module.h>
  29. #include <linux/errno.h>
  30. #include <linux/types.h>
  31. #include <linux/socket.h>
  32. #include <linux/sockios.h>
  33. #include <linux/net.h>
  34. #include <linux/in.h>
  35. #include <linux/in6.h>
  36. #include <linux/netdevice.h>
  37. #include <linux/init.h>
  38. #include <linux/ipsec.h>
  39. #include <linux/slab.h>
  40. #include <linux/ipv6.h>
  41. #include <linux/icmpv6.h>
  42. #include <linux/random.h>
  43. #include <linux/seq_file.h>
  44. #include <net/protocol.h>
  45. #include <net/ndisc.h>
  46. #include <net/ip.h>
  47. #include <net/ipv6.h>
  48. #include <net/transp_v6.h>
  49. #include <net/addrconf.h>
  50. #include <net/ip6_route.h>
  51. #include <net/inet_common.h>
  52. #include <net/inet_ecn.h>
  53. #include <net/sctp/sctp.h>
  54. #include <net/udp_tunnel.h>
  55. #include <linux/uaccess.h>
  56. static inline int sctp_v6_addr_match_len(union sctp_addr *s1,
  57. union sctp_addr *s2);
  58. static void sctp_v6_to_addr(union sctp_addr *addr, struct in6_addr *saddr,
  59. __be16 port);
  60. static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
  61. const union sctp_addr *addr2);
  62. /* Event handler for inet6 address addition/deletion events.
  63. * The sctp_local_addr_list needs to be protocted by a spin lock since
  64. * multiple notifiers (say IPv4 and IPv6) may be running at the same
  65. * time and thus corrupt the list.
  66. * The reader side is protected with RCU.
  67. */
  68. static int sctp_inet6addr_event(struct notifier_block *this, unsigned long ev,
  69. void *ptr)
  70. {
  71. struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
  72. struct sctp_sockaddr_entry *addr = NULL;
  73. struct sctp_sockaddr_entry *temp;
  74. struct net *net = dev_net(ifa->idev->dev);
  75. int found = 0;
  76. switch (ev) {
  77. case NETDEV_UP:
  78. addr = kzalloc(sizeof(*addr), GFP_ATOMIC);
  79. if (addr) {
  80. addr->a.v6.sin6_family = AF_INET6;
  81. addr->a.v6.sin6_addr = ifa->addr;
  82. addr->a.v6.sin6_scope_id = ifa->idev->dev->ifindex;
  83. addr->valid = 1;
  84. spin_lock_bh(&net->sctp.local_addr_lock);
  85. list_add_tail_rcu(&addr->list, &net->sctp.local_addr_list);
  86. sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_NEW);
  87. spin_unlock_bh(&net->sctp.local_addr_lock);
  88. }
  89. break;
  90. case NETDEV_DOWN:
  91. spin_lock_bh(&net->sctp.local_addr_lock);
  92. list_for_each_entry_safe(addr, temp,
  93. &net->sctp.local_addr_list, list) {
  94. if (addr->a.sa.sa_family == AF_INET6 &&
  95. ipv6_addr_equal(&addr->a.v6.sin6_addr,
  96. &ifa->addr) &&
  97. addr->a.v6.sin6_scope_id == ifa->idev->dev->ifindex) {
  98. sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_DEL);
  99. found = 1;
  100. addr->valid = 0;
  101. list_del_rcu(&addr->list);
  102. break;
  103. }
  104. }
  105. spin_unlock_bh(&net->sctp.local_addr_lock);
  106. if (found)
  107. kfree_rcu(addr, rcu);
  108. break;
  109. }
  110. return NOTIFY_DONE;
  111. }
  112. static struct notifier_block sctp_inet6addr_notifier = {
  113. .notifier_call = sctp_inet6addr_event,
  114. };
  115. static void sctp_v6_err_handle(struct sctp_transport *t, struct sk_buff *skb,
  116. __u8 type, __u8 code, __u32 info)
  117. {
  118. struct sctp_association *asoc = t->asoc;
  119. struct sock *sk = asoc->base.sk;
  120. int err = 0;
  121. switch (type) {
  122. case ICMPV6_PKT_TOOBIG:
  123. if (ip6_sk_accept_pmtu(sk))
  124. sctp_icmp_frag_needed(sk, asoc, t, info);
  125. return;
  126. case ICMPV6_PARAMPROB:
  127. if (ICMPV6_UNK_NEXTHDR == code) {
  128. sctp_icmp_proto_unreachable(sk, asoc, t);
  129. return;
  130. }
  131. break;
  132. case NDISC_REDIRECT:
  133. sctp_icmp_redirect(sk, t, skb);
  134. return;
  135. default:
  136. break;
  137. }
  138. icmpv6_err_convert(type, code, &err);
  139. if (!sock_owned_by_user(sk) && inet6_test_bit(RECVERR6, sk)) {
  140. sk->sk_err = err;
  141. sk_error_report(sk);
  142. } else {
  143. WRITE_ONCE(sk->sk_err_soft, err);
  144. }
  145. }
  146. /* ICMP error handler. */
  147. static int sctp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  148. u8 type, u8 code, int offset, __be32 info)
  149. {
  150. struct net *net = dev_net(skb->dev);
  151. struct sctp_transport *transport;
  152. struct sctp_association *asoc;
  153. __u16 saveip, savesctp;
  154. struct sock *sk;
  155. /* Fix up skb to look at the embedded net header. */
  156. saveip = skb->network_header;
  157. savesctp = skb->transport_header;
  158. skb_reset_network_header(skb);
  159. skb_set_transport_header(skb, offset);
  160. sk = sctp_err_lookup(net, AF_INET6, skb, sctp_hdr(skb), &asoc, &transport);
  161. /* Put back, the original pointers. */
  162. skb->network_header = saveip;
  163. skb->transport_header = savesctp;
  164. if (!sk) {
  165. __ICMP6_INC_STATS(net, __in6_dev_get(skb->dev), ICMP6_MIB_INERRORS);
  166. return -ENOENT;
  167. }
  168. sctp_v6_err_handle(transport, skb, type, code, ntohl(info));
  169. sctp_err_finish(sk, transport);
  170. return 0;
  171. }
  172. int sctp_udp_v6_err(struct sock *sk, struct sk_buff *skb)
  173. {
  174. struct net *net = dev_net(skb->dev);
  175. struct sctp_association *asoc;
  176. struct sctp_transport *t;
  177. struct icmp6hdr *hdr;
  178. __u32 info = 0;
  179. skb->transport_header += sizeof(struct udphdr);
  180. sk = sctp_err_lookup(net, AF_INET6, skb, sctp_hdr(skb), &asoc, &t);
  181. if (!sk) {
  182. __ICMP6_INC_STATS(net, __in6_dev_get(skb->dev), ICMP6_MIB_INERRORS);
  183. return -ENOENT;
  184. }
  185. skb->transport_header -= sizeof(struct udphdr);
  186. hdr = (struct icmp6hdr *)(skb_network_header(skb) - sizeof(struct icmp6hdr));
  187. if (hdr->icmp6_type == NDISC_REDIRECT) {
  188. /* can't be handled without outer ip6hdr known, leave it to udpv6_err */
  189. sctp_err_finish(sk, t);
  190. return 0;
  191. }
  192. if (hdr->icmp6_type == ICMPV6_PKT_TOOBIG)
  193. info = ntohl(hdr->icmp6_mtu);
  194. sctp_v6_err_handle(t, skb, hdr->icmp6_type, hdr->icmp6_code, info);
  195. sctp_err_finish(sk, t);
  196. return 1;
  197. }
  198. static int sctp_v6_xmit(struct sk_buff *skb, struct sctp_transport *t)
  199. {
  200. struct dst_entry *dst = dst_clone(t->dst);
  201. struct flowi6 *fl6 = &t->fl.u.ip6;
  202. struct sock *sk = skb->sk;
  203. struct ipv6_pinfo *np = inet6_sk(sk);
  204. __u8 tclass = np->tclass;
  205. __be32 label;
  206. pr_debug("%s: skb:%p, len:%d, src:%pI6 dst:%pI6\n", __func__, skb,
  207. skb->len, &fl6->saddr, &fl6->daddr);
  208. if (t->dscp & SCTP_DSCP_SET_MASK)
  209. tclass = t->dscp & SCTP_DSCP_VAL_MASK;
  210. if (INET_ECN_is_capable(tclass))
  211. IP6_ECN_flow_xmit(sk, fl6->flowlabel);
  212. if (!(t->param_flags & SPP_PMTUD_ENABLE))
  213. skb->ignore_df = 1;
  214. SCTP_INC_STATS(sock_net(sk), SCTP_MIB_OUTSCTPPACKS);
  215. if (!t->encap_port || !sctp_sk(sk)->udp_port) {
  216. int res;
  217. skb_dst_set(skb, dst);
  218. rcu_read_lock();
  219. res = ip6_xmit(sk, skb, fl6, sk->sk_mark,
  220. rcu_dereference(np->opt),
  221. tclass, READ_ONCE(sk->sk_priority));
  222. rcu_read_unlock();
  223. return res;
  224. }
  225. if (skb_is_gso(skb))
  226. skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
  227. skb->encapsulation = 1;
  228. skb_reset_inner_mac_header(skb);
  229. skb_reset_inner_transport_header(skb);
  230. skb_set_inner_ipproto(skb, IPPROTO_SCTP);
  231. label = ip6_make_flowlabel(sock_net(sk), skb, fl6->flowlabel, true, fl6);
  232. return udp_tunnel6_xmit_skb(dst, sk, skb, NULL, &fl6->saddr,
  233. &fl6->daddr, tclass, ip6_dst_hoplimit(dst),
  234. label, sctp_sk(sk)->udp_port, t->encap_port, false);
  235. }
  236. /* Returns the dst cache entry for the given source and destination ip
  237. * addresses.
  238. */
  239. static void sctp_v6_get_dst(struct sctp_transport *t, union sctp_addr *saddr,
  240. struct flowi *fl, struct sock *sk)
  241. {
  242. struct sctp_association *asoc = t->asoc;
  243. struct dst_entry *dst = NULL;
  244. struct flowi _fl;
  245. struct flowi6 *fl6 = &_fl.u.ip6;
  246. struct sctp_bind_addr *bp;
  247. struct ipv6_pinfo *np = inet6_sk(sk);
  248. struct sctp_sockaddr_entry *laddr;
  249. union sctp_addr *daddr = &t->ipaddr;
  250. union sctp_addr dst_saddr;
  251. struct in6_addr *final_p, final;
  252. enum sctp_scope scope;
  253. __u8 matchlen = 0;
  254. memset(&_fl, 0, sizeof(_fl));
  255. fl6->daddr = daddr->v6.sin6_addr;
  256. fl6->fl6_dport = daddr->v6.sin6_port;
  257. fl6->flowi6_proto = IPPROTO_SCTP;
  258. if (ipv6_addr_type(&daddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
  259. fl6->flowi6_oif = daddr->v6.sin6_scope_id;
  260. else if (asoc)
  261. fl6->flowi6_oif = asoc->base.sk->sk_bound_dev_if;
  262. if (t->flowlabel & SCTP_FLOWLABEL_SET_MASK)
  263. fl6->flowlabel = htonl(t->flowlabel & SCTP_FLOWLABEL_VAL_MASK);
  264. if (inet6_test_bit(SNDFLOW, sk) &&
  265. (fl6->flowlabel & IPV6_FLOWLABEL_MASK)) {
  266. struct ip6_flowlabel *flowlabel;
  267. flowlabel = fl6_sock_lookup(sk, fl6->flowlabel);
  268. if (IS_ERR(flowlabel))
  269. goto out;
  270. fl6_sock_release(flowlabel);
  271. }
  272. pr_debug("%s: dst=%pI6 ", __func__, &fl6->daddr);
  273. if (asoc)
  274. fl6->fl6_sport = htons(asoc->base.bind_addr.port);
  275. if (saddr) {
  276. fl6->saddr = saddr->v6.sin6_addr;
  277. if (!fl6->fl6_sport)
  278. fl6->fl6_sport = saddr->v6.sin6_port;
  279. pr_debug("src=%pI6 - ", &fl6->saddr);
  280. }
  281. rcu_read_lock();
  282. final_p = fl6_update_dst(fl6, rcu_dereference(np->opt), &final);
  283. rcu_read_unlock();
  284. dst = ip6_dst_lookup_flow(sock_net(sk), sk, fl6, final_p);
  285. if (!asoc || saddr) {
  286. t->dst = dst;
  287. memcpy(fl, &_fl, sizeof(_fl));
  288. goto out;
  289. }
  290. bp = &asoc->base.bind_addr;
  291. scope = sctp_scope(daddr);
  292. /* ip6_dst_lookup has filled in the fl6->saddr for us. Check
  293. * to see if we can use it.
  294. */
  295. if (!IS_ERR(dst)) {
  296. /* Walk through the bind address list and look for a bind
  297. * address that matches the source address of the returned dst.
  298. */
  299. sctp_v6_to_addr(&dst_saddr, &fl6->saddr, htons(bp->port));
  300. rcu_read_lock();
  301. list_for_each_entry_rcu(laddr, &bp->address_list, list) {
  302. if (!laddr->valid || laddr->state == SCTP_ADDR_DEL ||
  303. (laddr->state != SCTP_ADDR_SRC &&
  304. !asoc->src_out_of_asoc_ok))
  305. continue;
  306. /* Do not compare against v4 addrs */
  307. if ((laddr->a.sa.sa_family == AF_INET6) &&
  308. (sctp_v6_cmp_addr(&dst_saddr, &laddr->a))) {
  309. rcu_read_unlock();
  310. t->dst = dst;
  311. memcpy(fl, &_fl, sizeof(_fl));
  312. goto out;
  313. }
  314. }
  315. rcu_read_unlock();
  316. /* None of the bound addresses match the source address of the
  317. * dst. So release it.
  318. */
  319. dst_release(dst);
  320. dst = NULL;
  321. }
  322. /* Walk through the bind address list and try to get the
  323. * best source address for a given destination.
  324. */
  325. rcu_read_lock();
  326. list_for_each_entry_rcu(laddr, &bp->address_list, list) {
  327. struct dst_entry *bdst;
  328. __u8 bmatchlen;
  329. if (!laddr->valid ||
  330. laddr->state != SCTP_ADDR_SRC ||
  331. laddr->a.sa.sa_family != AF_INET6 ||
  332. scope > sctp_scope(&laddr->a))
  333. continue;
  334. fl6->saddr = laddr->a.v6.sin6_addr;
  335. fl6->fl6_sport = laddr->a.v6.sin6_port;
  336. final_p = fl6_update_dst(fl6, rcu_dereference(np->opt), &final);
  337. bdst = ip6_dst_lookup_flow(sock_net(sk), sk, fl6, final_p);
  338. if (IS_ERR(bdst))
  339. continue;
  340. if (ipv6_chk_addr(dev_net(bdst->dev),
  341. &laddr->a.v6.sin6_addr, bdst->dev, 1)) {
  342. if (!IS_ERR_OR_NULL(dst))
  343. dst_release(dst);
  344. dst = bdst;
  345. t->dst = dst;
  346. memcpy(fl, &_fl, sizeof(_fl));
  347. break;
  348. }
  349. bmatchlen = sctp_v6_addr_match_len(daddr, &laddr->a);
  350. if (matchlen > bmatchlen) {
  351. dst_release(bdst);
  352. continue;
  353. }
  354. if (!IS_ERR_OR_NULL(dst))
  355. dst_release(dst);
  356. dst = bdst;
  357. matchlen = bmatchlen;
  358. t->dst = dst;
  359. memcpy(fl, &_fl, sizeof(_fl));
  360. }
  361. rcu_read_unlock();
  362. out:
  363. if (!IS_ERR_OR_NULL(dst)) {
  364. struct rt6_info *rt;
  365. rt = dst_rt6_info(dst);
  366. t->dst_cookie = rt6_get_cookie(rt);
  367. pr_debug("rt6_dst:%pI6/%d rt6_src:%pI6\n",
  368. &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
  369. &fl->u.ip6.saddr);
  370. } else {
  371. t->dst = NULL;
  372. pr_debug("no route\n");
  373. }
  374. }
  375. /* Returns the number of consecutive initial bits that match in the 2 ipv6
  376. * addresses.
  377. */
  378. static inline int sctp_v6_addr_match_len(union sctp_addr *s1,
  379. union sctp_addr *s2)
  380. {
  381. return ipv6_addr_diff(&s1->v6.sin6_addr, &s2->v6.sin6_addr);
  382. }
  383. /* Fills in the source address(saddr) based on the destination address(daddr)
  384. * and asoc's bind address list.
  385. */
  386. static void sctp_v6_get_saddr(struct sctp_sock *sk,
  387. struct sctp_transport *t,
  388. struct flowi *fl)
  389. {
  390. struct flowi6 *fl6 = &fl->u.ip6;
  391. union sctp_addr *saddr = &t->saddr;
  392. pr_debug("%s: asoc:%p dst:%p\n", __func__, t->asoc, t->dst);
  393. if (t->dst) {
  394. saddr->v6.sin6_family = AF_INET6;
  395. saddr->v6.sin6_addr = fl6->saddr;
  396. }
  397. }
  398. /* Make a copy of all potential local addresses. */
  399. static void sctp_v6_copy_addrlist(struct list_head *addrlist,
  400. struct net_device *dev)
  401. {
  402. struct inet6_dev *in6_dev;
  403. struct inet6_ifaddr *ifp;
  404. struct sctp_sockaddr_entry *addr;
  405. rcu_read_lock();
  406. if ((in6_dev = __in6_dev_get(dev)) == NULL) {
  407. rcu_read_unlock();
  408. return;
  409. }
  410. read_lock_bh(&in6_dev->lock);
  411. list_for_each_entry(ifp, &in6_dev->addr_list, if_list) {
  412. /* Add the address to the local list. */
  413. addr = kzalloc(sizeof(*addr), GFP_ATOMIC);
  414. if (addr) {
  415. addr->a.v6.sin6_family = AF_INET6;
  416. addr->a.v6.sin6_addr = ifp->addr;
  417. addr->a.v6.sin6_scope_id = dev->ifindex;
  418. addr->valid = 1;
  419. INIT_LIST_HEAD(&addr->list);
  420. list_add_tail(&addr->list, addrlist);
  421. }
  422. }
  423. read_unlock_bh(&in6_dev->lock);
  424. rcu_read_unlock();
  425. }
  426. /* Copy over any ip options */
  427. static void sctp_v6_copy_ip_options(struct sock *sk, struct sock *newsk)
  428. {
  429. struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
  430. struct ipv6_txoptions *opt;
  431. newnp = inet6_sk(newsk);
  432. rcu_read_lock();
  433. opt = rcu_dereference(np->opt);
  434. if (opt) {
  435. opt = ipv6_dup_options(newsk, opt);
  436. if (!opt)
  437. pr_err("%s: Failed to copy ip options\n", __func__);
  438. }
  439. RCU_INIT_POINTER(newnp->opt, opt);
  440. rcu_read_unlock();
  441. }
  442. /* Account for the IP options */
  443. static int sctp_v6_ip_options_len(struct sock *sk)
  444. {
  445. struct ipv6_pinfo *np = inet6_sk(sk);
  446. struct ipv6_txoptions *opt;
  447. int len = 0;
  448. rcu_read_lock();
  449. opt = rcu_dereference(np->opt);
  450. if (opt)
  451. len = opt->opt_flen + opt->opt_nflen;
  452. rcu_read_unlock();
  453. return len;
  454. }
  455. /* Initialize a sockaddr_storage from in incoming skb. */
  456. static void sctp_v6_from_skb(union sctp_addr *addr, struct sk_buff *skb,
  457. int is_saddr)
  458. {
  459. /* Always called on head skb, so this is safe */
  460. struct sctphdr *sh = sctp_hdr(skb);
  461. struct sockaddr_in6 *sa = &addr->v6;
  462. addr->v6.sin6_family = AF_INET6;
  463. addr->v6.sin6_flowinfo = 0; /* FIXME */
  464. addr->v6.sin6_scope_id = ((struct inet6_skb_parm *)skb->cb)->iif;
  465. if (is_saddr) {
  466. sa->sin6_port = sh->source;
  467. sa->sin6_addr = ipv6_hdr(skb)->saddr;
  468. } else {
  469. sa->sin6_port = sh->dest;
  470. sa->sin6_addr = ipv6_hdr(skb)->daddr;
  471. }
  472. }
  473. /* Initialize an sctp_addr from a socket. */
  474. static void sctp_v6_from_sk(union sctp_addr *addr, struct sock *sk)
  475. {
  476. addr->v6.sin6_family = AF_INET6;
  477. addr->v6.sin6_port = 0;
  478. addr->v6.sin6_addr = sk->sk_v6_rcv_saddr;
  479. }
  480. /* Initialize sk->sk_rcv_saddr from sctp_addr. */
  481. static void sctp_v6_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
  482. {
  483. if (addr->sa.sa_family == AF_INET) {
  484. sk->sk_v6_rcv_saddr.s6_addr32[0] = 0;
  485. sk->sk_v6_rcv_saddr.s6_addr32[1] = 0;
  486. sk->sk_v6_rcv_saddr.s6_addr32[2] = htonl(0x0000ffff);
  487. sk->sk_v6_rcv_saddr.s6_addr32[3] =
  488. addr->v4.sin_addr.s_addr;
  489. } else {
  490. sk->sk_v6_rcv_saddr = addr->v6.sin6_addr;
  491. }
  492. }
  493. /* Initialize sk->sk_daddr from sctp_addr. */
  494. static void sctp_v6_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
  495. {
  496. if (addr->sa.sa_family == AF_INET) {
  497. sk->sk_v6_daddr.s6_addr32[0] = 0;
  498. sk->sk_v6_daddr.s6_addr32[1] = 0;
  499. sk->sk_v6_daddr.s6_addr32[2] = htonl(0x0000ffff);
  500. sk->sk_v6_daddr.s6_addr32[3] = addr->v4.sin_addr.s_addr;
  501. } else {
  502. sk->sk_v6_daddr = addr->v6.sin6_addr;
  503. }
  504. }
  505. /* Initialize a sctp_addr from an address parameter. */
  506. static bool sctp_v6_from_addr_param(union sctp_addr *addr,
  507. union sctp_addr_param *param,
  508. __be16 port, int iif)
  509. {
  510. if (ntohs(param->v6.param_hdr.length) < sizeof(struct sctp_ipv6addr_param))
  511. return false;
  512. addr->v6.sin6_family = AF_INET6;
  513. addr->v6.sin6_port = port;
  514. addr->v6.sin6_flowinfo = 0; /* BUG */
  515. addr->v6.sin6_addr = param->v6.addr;
  516. addr->v6.sin6_scope_id = iif;
  517. return true;
  518. }
  519. /* Initialize an address parameter from a sctp_addr and return the length
  520. * of the address parameter.
  521. */
  522. static int sctp_v6_to_addr_param(const union sctp_addr *addr,
  523. union sctp_addr_param *param)
  524. {
  525. int length = sizeof(struct sctp_ipv6addr_param);
  526. param->v6.param_hdr.type = SCTP_PARAM_IPV6_ADDRESS;
  527. param->v6.param_hdr.length = htons(length);
  528. param->v6.addr = addr->v6.sin6_addr;
  529. return length;
  530. }
  531. /* Initialize a sctp_addr from struct in6_addr. */
  532. static void sctp_v6_to_addr(union sctp_addr *addr, struct in6_addr *saddr,
  533. __be16 port)
  534. {
  535. addr->sa.sa_family = AF_INET6;
  536. addr->v6.sin6_port = port;
  537. addr->v6.sin6_flowinfo = 0;
  538. addr->v6.sin6_addr = *saddr;
  539. addr->v6.sin6_scope_id = 0;
  540. }
  541. static int __sctp_v6_cmp_addr(const union sctp_addr *addr1,
  542. const union sctp_addr *addr2)
  543. {
  544. if (addr1->sa.sa_family != addr2->sa.sa_family) {
  545. if (addr1->sa.sa_family == AF_INET &&
  546. addr2->sa.sa_family == AF_INET6 &&
  547. ipv6_addr_v4mapped(&addr2->v6.sin6_addr) &&
  548. addr2->v6.sin6_addr.s6_addr32[3] ==
  549. addr1->v4.sin_addr.s_addr)
  550. return 1;
  551. if (addr2->sa.sa_family == AF_INET &&
  552. addr1->sa.sa_family == AF_INET6 &&
  553. ipv6_addr_v4mapped(&addr1->v6.sin6_addr) &&
  554. addr1->v6.sin6_addr.s6_addr32[3] ==
  555. addr2->v4.sin_addr.s_addr)
  556. return 1;
  557. return 0;
  558. }
  559. if (!ipv6_addr_equal(&addr1->v6.sin6_addr, &addr2->v6.sin6_addr))
  560. return 0;
  561. /* If this is a linklocal address, compare the scope_id. */
  562. if ((ipv6_addr_type(&addr1->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) &&
  563. addr1->v6.sin6_scope_id && addr2->v6.sin6_scope_id &&
  564. addr1->v6.sin6_scope_id != addr2->v6.sin6_scope_id)
  565. return 0;
  566. return 1;
  567. }
  568. /* Compare addresses exactly.
  569. * v4-mapped-v6 is also in consideration.
  570. */
  571. static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
  572. const union sctp_addr *addr2)
  573. {
  574. return __sctp_v6_cmp_addr(addr1, addr2) &&
  575. addr1->v6.sin6_port == addr2->v6.sin6_port;
  576. }
  577. /* Initialize addr struct to INADDR_ANY. */
  578. static void sctp_v6_inaddr_any(union sctp_addr *addr, __be16 port)
  579. {
  580. memset(addr, 0x00, sizeof(union sctp_addr));
  581. addr->v6.sin6_family = AF_INET6;
  582. addr->v6.sin6_port = port;
  583. }
  584. /* Is this a wildcard address? */
  585. static int sctp_v6_is_any(const union sctp_addr *addr)
  586. {
  587. return ipv6_addr_any(&addr->v6.sin6_addr);
  588. }
  589. /* Should this be available for binding? */
  590. static int sctp_v6_available(union sctp_addr *addr, struct sctp_sock *sp)
  591. {
  592. const struct in6_addr *in6 = (const struct in6_addr *)&addr->v6.sin6_addr;
  593. struct sock *sk = &sp->inet.sk;
  594. struct net *net = sock_net(sk);
  595. struct net_device *dev = NULL;
  596. int type, res, bound_dev_if;
  597. type = ipv6_addr_type(in6);
  598. if (IPV6_ADDR_ANY == type)
  599. return 1;
  600. if (type == IPV6_ADDR_MAPPED) {
  601. if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
  602. return 0;
  603. sctp_v6_map_v4(addr);
  604. return sctp_get_af_specific(AF_INET)->available(addr, sp);
  605. }
  606. if (!(type & IPV6_ADDR_UNICAST))
  607. return 0;
  608. rcu_read_lock();
  609. bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
  610. if (bound_dev_if) {
  611. res = 0;
  612. dev = dev_get_by_index_rcu(net, bound_dev_if);
  613. if (!dev)
  614. goto out;
  615. }
  616. res = ipv6_can_nonlocal_bind(net, &sp->inet) ||
  617. ipv6_chk_addr(net, in6, dev, 0);
  618. out:
  619. rcu_read_unlock();
  620. return res;
  621. }
  622. /* This function checks if the address is a valid address to be used for
  623. * SCTP.
  624. *
  625. * Output:
  626. * Return 0 - If the address is a non-unicast or an illegal address.
  627. * Return 1 - If the address is a unicast.
  628. */
  629. static int sctp_v6_addr_valid(union sctp_addr *addr,
  630. struct sctp_sock *sp,
  631. const struct sk_buff *skb)
  632. {
  633. int ret = ipv6_addr_type(&addr->v6.sin6_addr);
  634. /* Support v4-mapped-v6 address. */
  635. if (ret == IPV6_ADDR_MAPPED) {
  636. /* Note: This routine is used in input, so v4-mapped-v6
  637. * are disallowed here when there is no sctp_sock.
  638. */
  639. if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
  640. return 0;
  641. sctp_v6_map_v4(addr);
  642. return sctp_get_af_specific(AF_INET)->addr_valid(addr, sp, skb);
  643. }
  644. /* Is this a non-unicast address */
  645. if (!(ret & IPV6_ADDR_UNICAST))
  646. return 0;
  647. return 1;
  648. }
  649. /* What is the scope of 'addr'? */
  650. static enum sctp_scope sctp_v6_scope(union sctp_addr *addr)
  651. {
  652. enum sctp_scope retval;
  653. int v6scope;
  654. /* The IPv6 scope is really a set of bit fields.
  655. * See IFA_* in <net/if_inet6.h>. Map to a generic SCTP scope.
  656. */
  657. v6scope = ipv6_addr_scope(&addr->v6.sin6_addr);
  658. switch (v6scope) {
  659. case IFA_HOST:
  660. retval = SCTP_SCOPE_LOOPBACK;
  661. break;
  662. case IFA_LINK:
  663. retval = SCTP_SCOPE_LINK;
  664. break;
  665. case IFA_SITE:
  666. retval = SCTP_SCOPE_PRIVATE;
  667. break;
  668. default:
  669. retval = SCTP_SCOPE_GLOBAL;
  670. break;
  671. }
  672. return retval;
  673. }
  674. /* Create and initialize a new sk for the socket to be returned by accept(). */
  675. static struct sock *sctp_v6_create_accept_sk(struct sock *sk,
  676. struct sctp_association *asoc,
  677. bool kern)
  678. {
  679. struct sock *newsk;
  680. struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
  681. struct sctp6_sock *newsctp6sk;
  682. newsk = sk_alloc(sock_net(sk), PF_INET6, GFP_KERNEL, sk->sk_prot, kern);
  683. if (!newsk)
  684. goto out;
  685. sock_init_data(NULL, newsk);
  686. sctp_copy_sock(newsk, sk, asoc);
  687. sock_reset_flag(sk, SOCK_ZAPPED);
  688. newsctp6sk = (struct sctp6_sock *)newsk;
  689. inet_sk(newsk)->pinet6 = &newsctp6sk->inet6;
  690. sctp_sk(newsk)->v4mapped = sctp_sk(sk)->v4mapped;
  691. newnp = inet6_sk(newsk);
  692. memcpy(newnp, np, sizeof(struct ipv6_pinfo));
  693. newnp->ipv6_mc_list = NULL;
  694. newnp->ipv6_ac_list = NULL;
  695. newnp->ipv6_fl_list = NULL;
  696. sctp_v6_copy_ip_options(sk, newsk);
  697. /* Initialize sk's sport, dport, rcv_saddr and daddr for getsockname()
  698. * and getpeername().
  699. */
  700. sctp_v6_to_sk_daddr(&asoc->peer.primary_addr, newsk);
  701. newsk->sk_v6_rcv_saddr = sk->sk_v6_rcv_saddr;
  702. if (newsk->sk_prot->init(newsk)) {
  703. sk_common_release(newsk);
  704. newsk = NULL;
  705. }
  706. out:
  707. return newsk;
  708. }
  709. /* Format a sockaddr for return to user space. This makes sure the return is
  710. * AF_INET or AF_INET6 depending on the SCTP_I_WANT_MAPPED_V4_ADDR option.
  711. */
  712. static int sctp_v6_addr_to_user(struct sctp_sock *sp, union sctp_addr *addr)
  713. {
  714. if (sp->v4mapped) {
  715. if (addr->sa.sa_family == AF_INET)
  716. sctp_v4_map_v6(addr);
  717. } else {
  718. if (addr->sa.sa_family == AF_INET6 &&
  719. ipv6_addr_v4mapped(&addr->v6.sin6_addr))
  720. sctp_v6_map_v4(addr);
  721. }
  722. if (addr->sa.sa_family == AF_INET) {
  723. memset(addr->v4.sin_zero, 0, sizeof(addr->v4.sin_zero));
  724. return sizeof(struct sockaddr_in);
  725. }
  726. return sizeof(struct sockaddr_in6);
  727. }
  728. /* Where did this skb come from? */
  729. static int sctp_v6_skb_iif(const struct sk_buff *skb)
  730. {
  731. return inet6_iif(skb);
  732. }
  733. static int sctp_v6_skb_sdif(const struct sk_buff *skb)
  734. {
  735. return inet6_sdif(skb);
  736. }
  737. /* Was this packet marked by Explicit Congestion Notification? */
  738. static int sctp_v6_is_ce(const struct sk_buff *skb)
  739. {
  740. return *((__u32 *)(ipv6_hdr(skb))) & (__force __u32)htonl(1 << 20);
  741. }
  742. /* Dump the v6 addr to the seq file. */
  743. static void sctp_v6_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
  744. {
  745. seq_printf(seq, "%pI6 ", &addr->v6.sin6_addr);
  746. }
  747. static void sctp_v6_ecn_capable(struct sock *sk)
  748. {
  749. inet6_sk(sk)->tclass |= INET_ECN_ECT_0;
  750. }
  751. /* Initialize a PF_INET msgname from a ulpevent. */
  752. static void sctp_inet6_event_msgname(struct sctp_ulpevent *event,
  753. char *msgname, int *addrlen)
  754. {
  755. union sctp_addr *addr;
  756. struct sctp_association *asoc;
  757. union sctp_addr *paddr;
  758. if (!msgname)
  759. return;
  760. addr = (union sctp_addr *)msgname;
  761. asoc = event->asoc;
  762. paddr = &asoc->peer.primary_addr;
  763. if (paddr->sa.sa_family == AF_INET) {
  764. addr->v4.sin_family = AF_INET;
  765. addr->v4.sin_port = htons(asoc->peer.port);
  766. addr->v4.sin_addr = paddr->v4.sin_addr;
  767. } else {
  768. addr->v6.sin6_family = AF_INET6;
  769. addr->v6.sin6_flowinfo = 0;
  770. if (ipv6_addr_type(&paddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
  771. addr->v6.sin6_scope_id = paddr->v6.sin6_scope_id;
  772. else
  773. addr->v6.sin6_scope_id = 0;
  774. addr->v6.sin6_port = htons(asoc->peer.port);
  775. addr->v6.sin6_addr = paddr->v6.sin6_addr;
  776. }
  777. *addrlen = sctp_v6_addr_to_user(sctp_sk(asoc->base.sk), addr);
  778. }
  779. /* Initialize a msg_name from an inbound skb. */
  780. static void sctp_inet6_skb_msgname(struct sk_buff *skb, char *msgname,
  781. int *addr_len)
  782. {
  783. union sctp_addr *addr;
  784. struct sctphdr *sh;
  785. if (!msgname)
  786. return;
  787. addr = (union sctp_addr *)msgname;
  788. sh = sctp_hdr(skb);
  789. if (ip_hdr(skb)->version == 4) {
  790. addr->v4.sin_family = AF_INET;
  791. addr->v4.sin_port = sh->source;
  792. addr->v4.sin_addr.s_addr = ip_hdr(skb)->saddr;
  793. } else {
  794. addr->v6.sin6_family = AF_INET6;
  795. addr->v6.sin6_flowinfo = 0;
  796. addr->v6.sin6_port = sh->source;
  797. addr->v6.sin6_addr = ipv6_hdr(skb)->saddr;
  798. if (ipv6_addr_type(&addr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
  799. addr->v6.sin6_scope_id = sctp_v6_skb_iif(skb);
  800. else
  801. addr->v6.sin6_scope_id = 0;
  802. }
  803. *addr_len = sctp_v6_addr_to_user(sctp_sk(skb->sk), addr);
  804. }
  805. /* Do we support this AF? */
  806. static int sctp_inet6_af_supported(sa_family_t family, struct sctp_sock *sp)
  807. {
  808. switch (family) {
  809. case AF_INET6:
  810. return 1;
  811. /* v4-mapped-v6 addresses */
  812. case AF_INET:
  813. if (!ipv6_only_sock(sctp_opt2sk(sp)))
  814. return 1;
  815. fallthrough;
  816. default:
  817. return 0;
  818. }
  819. }
  820. /* Address matching with wildcards allowed. This extra level
  821. * of indirection lets us choose whether a PF_INET6 should
  822. * disallow any v4 addresses if we so choose.
  823. */
  824. static int sctp_inet6_cmp_addr(const union sctp_addr *addr1,
  825. const union sctp_addr *addr2,
  826. struct sctp_sock *opt)
  827. {
  828. struct sock *sk = sctp_opt2sk(opt);
  829. struct sctp_af *af1, *af2;
  830. af1 = sctp_get_af_specific(addr1->sa.sa_family);
  831. af2 = sctp_get_af_specific(addr2->sa.sa_family);
  832. if (!af1 || !af2)
  833. return 0;
  834. /* If the socket is IPv6 only, v4 addrs will not match */
  835. if (ipv6_only_sock(sk) && af1 != af2)
  836. return 0;
  837. /* Today, wildcard AF_INET/AF_INET6. */
  838. if (sctp_is_any(sk, addr1) || sctp_is_any(sk, addr2))
  839. return 1;
  840. if (addr1->sa.sa_family == AF_INET && addr2->sa.sa_family == AF_INET)
  841. return addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr;
  842. return __sctp_v6_cmp_addr(addr1, addr2);
  843. }
  844. /* Verify that the provided sockaddr looks bindable. Common verification,
  845. * has already been taken care of.
  846. */
  847. static int sctp_inet6_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
  848. {
  849. struct sctp_af *af;
  850. /* ASSERT: address family has already been verified. */
  851. if (addr->sa.sa_family != AF_INET6)
  852. af = sctp_get_af_specific(addr->sa.sa_family);
  853. else {
  854. int type = ipv6_addr_type(&addr->v6.sin6_addr);
  855. struct net_device *dev;
  856. if (type & IPV6_ADDR_LINKLOCAL) {
  857. struct net *net;
  858. if (!addr->v6.sin6_scope_id)
  859. return 0;
  860. net = sock_net(&opt->inet.sk);
  861. rcu_read_lock();
  862. dev = dev_get_by_index_rcu(net, addr->v6.sin6_scope_id);
  863. if (!dev || !(ipv6_can_nonlocal_bind(net, &opt->inet) ||
  864. ipv6_chk_addr(net, &addr->v6.sin6_addr,
  865. dev, 0))) {
  866. rcu_read_unlock();
  867. return 0;
  868. }
  869. rcu_read_unlock();
  870. }
  871. af = opt->pf->af;
  872. }
  873. return af->available(addr, opt);
  874. }
  875. /* Verify that the provided sockaddr looks sendable. Common verification,
  876. * has already been taken care of.
  877. */
  878. static int sctp_inet6_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
  879. {
  880. struct sctp_af *af = NULL;
  881. /* ASSERT: address family has already been verified. */
  882. if (addr->sa.sa_family != AF_INET6)
  883. af = sctp_get_af_specific(addr->sa.sa_family);
  884. else {
  885. int type = ipv6_addr_type(&addr->v6.sin6_addr);
  886. struct net_device *dev;
  887. if (type & IPV6_ADDR_LINKLOCAL) {
  888. if (!addr->v6.sin6_scope_id)
  889. return 0;
  890. rcu_read_lock();
  891. dev = dev_get_by_index_rcu(sock_net(&opt->inet.sk),
  892. addr->v6.sin6_scope_id);
  893. rcu_read_unlock();
  894. if (!dev)
  895. return 0;
  896. }
  897. af = opt->pf->af;
  898. }
  899. return af != NULL;
  900. }
  901. /* Fill in Supported Address Type information for INIT and INIT-ACK
  902. * chunks. Note: In the future, we may want to look at sock options
  903. * to determine whether a PF_INET6 socket really wants to have IPV4
  904. * addresses.
  905. * Returns number of addresses supported.
  906. */
  907. static int sctp_inet6_supported_addrs(const struct sctp_sock *opt,
  908. __be16 *types)
  909. {
  910. types[0] = SCTP_PARAM_IPV6_ADDRESS;
  911. if (!opt || !ipv6_only_sock(sctp_opt2sk(opt))) {
  912. types[1] = SCTP_PARAM_IPV4_ADDRESS;
  913. return 2;
  914. }
  915. return 1;
  916. }
  917. /* Handle SCTP_I_WANT_MAPPED_V4_ADDR for getpeername() and getsockname() */
  918. static int sctp_getname(struct socket *sock, struct sockaddr *uaddr,
  919. int peer)
  920. {
  921. int rc;
  922. rc = inet6_getname(sock, uaddr, peer);
  923. if (rc < 0)
  924. return rc;
  925. rc = sctp_v6_addr_to_user(sctp_sk(sock->sk),
  926. (union sctp_addr *)uaddr);
  927. return rc;
  928. }
  929. static const struct proto_ops inet6_seqpacket_ops = {
  930. .family = PF_INET6,
  931. .owner = THIS_MODULE,
  932. .release = inet6_release,
  933. .bind = inet6_bind,
  934. .connect = sctp_inet_connect,
  935. .socketpair = sock_no_socketpair,
  936. .accept = inet_accept,
  937. .getname = sctp_getname,
  938. .poll = sctp_poll,
  939. .ioctl = inet6_ioctl,
  940. .gettstamp = sock_gettstamp,
  941. .listen = sctp_inet_listen,
  942. .shutdown = inet_shutdown,
  943. .setsockopt = sock_common_setsockopt,
  944. .getsockopt = sock_common_getsockopt,
  945. .sendmsg = inet_sendmsg,
  946. .recvmsg = inet_recvmsg,
  947. .mmap = sock_no_mmap,
  948. #ifdef CONFIG_COMPAT
  949. .compat_ioctl = inet6_compat_ioctl,
  950. #endif
  951. };
  952. static struct inet_protosw sctpv6_seqpacket_protosw = {
  953. .type = SOCK_SEQPACKET,
  954. .protocol = IPPROTO_SCTP,
  955. .prot = &sctpv6_prot,
  956. .ops = &inet6_seqpacket_ops,
  957. .flags = SCTP_PROTOSW_FLAG
  958. };
  959. static struct inet_protosw sctpv6_stream_protosw = {
  960. .type = SOCK_STREAM,
  961. .protocol = IPPROTO_SCTP,
  962. .prot = &sctpv6_prot,
  963. .ops = &inet6_seqpacket_ops,
  964. .flags = SCTP_PROTOSW_FLAG,
  965. };
  966. static int sctp6_rcv(struct sk_buff *skb)
  967. {
  968. SCTP_INPUT_CB(skb)->encap_port = 0;
  969. return sctp_rcv(skb) ? -1 : 0;
  970. }
  971. static const struct inet6_protocol sctpv6_protocol = {
  972. .handler = sctp6_rcv,
  973. .err_handler = sctp_v6_err,
  974. .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
  975. };
  976. static struct sctp_af sctp_af_inet6 = {
  977. .sa_family = AF_INET6,
  978. .sctp_xmit = sctp_v6_xmit,
  979. .setsockopt = ipv6_setsockopt,
  980. .getsockopt = ipv6_getsockopt,
  981. .get_dst = sctp_v6_get_dst,
  982. .get_saddr = sctp_v6_get_saddr,
  983. .copy_addrlist = sctp_v6_copy_addrlist,
  984. .from_skb = sctp_v6_from_skb,
  985. .from_sk = sctp_v6_from_sk,
  986. .from_addr_param = sctp_v6_from_addr_param,
  987. .to_addr_param = sctp_v6_to_addr_param,
  988. .cmp_addr = sctp_v6_cmp_addr,
  989. .scope = sctp_v6_scope,
  990. .addr_valid = sctp_v6_addr_valid,
  991. .inaddr_any = sctp_v6_inaddr_any,
  992. .is_any = sctp_v6_is_any,
  993. .available = sctp_v6_available,
  994. .skb_iif = sctp_v6_skb_iif,
  995. .skb_sdif = sctp_v6_skb_sdif,
  996. .is_ce = sctp_v6_is_ce,
  997. .seq_dump_addr = sctp_v6_seq_dump_addr,
  998. .ecn_capable = sctp_v6_ecn_capable,
  999. .net_header_len = sizeof(struct ipv6hdr),
  1000. .sockaddr_len = sizeof(struct sockaddr_in6),
  1001. .ip_options_len = sctp_v6_ip_options_len,
  1002. };
  1003. static struct sctp_pf sctp_pf_inet6 = {
  1004. .event_msgname = sctp_inet6_event_msgname,
  1005. .skb_msgname = sctp_inet6_skb_msgname,
  1006. .af_supported = sctp_inet6_af_supported,
  1007. .cmp_addr = sctp_inet6_cmp_addr,
  1008. .bind_verify = sctp_inet6_bind_verify,
  1009. .send_verify = sctp_inet6_send_verify,
  1010. .supported_addrs = sctp_inet6_supported_addrs,
  1011. .create_accept_sk = sctp_v6_create_accept_sk,
  1012. .addr_to_user = sctp_v6_addr_to_user,
  1013. .to_sk_saddr = sctp_v6_to_sk_saddr,
  1014. .to_sk_daddr = sctp_v6_to_sk_daddr,
  1015. .copy_ip_options = sctp_v6_copy_ip_options,
  1016. .af = &sctp_af_inet6,
  1017. };
  1018. /* Initialize IPv6 support and register with socket layer. */
  1019. void sctp_v6_pf_init(void)
  1020. {
  1021. /* Register the SCTP specific PF_INET6 functions. */
  1022. sctp_register_pf(&sctp_pf_inet6, PF_INET6);
  1023. /* Register the SCTP specific AF_INET6 functions. */
  1024. sctp_register_af(&sctp_af_inet6);
  1025. }
  1026. void sctp_v6_pf_exit(void)
  1027. {
  1028. list_del(&sctp_af_inet6.list);
  1029. }
  1030. /* Initialize IPv6 support and register with socket layer. */
  1031. int sctp_v6_protosw_init(void)
  1032. {
  1033. int rc;
  1034. rc = proto_register(&sctpv6_prot, 1);
  1035. if (rc)
  1036. return rc;
  1037. /* Add SCTPv6(UDP and TCP style) to inetsw6 linked list. */
  1038. inet6_register_protosw(&sctpv6_seqpacket_protosw);
  1039. inet6_register_protosw(&sctpv6_stream_protosw);
  1040. return 0;
  1041. }
  1042. void sctp_v6_protosw_exit(void)
  1043. {
  1044. inet6_unregister_protosw(&sctpv6_seqpacket_protosw);
  1045. inet6_unregister_protosw(&sctpv6_stream_protosw);
  1046. proto_unregister(&sctpv6_prot);
  1047. }
  1048. /* Register with inet6 layer. */
  1049. int sctp_v6_add_protocol(void)
  1050. {
  1051. /* Register notifier for inet6 address additions/deletions. */
  1052. register_inet6addr_notifier(&sctp_inet6addr_notifier);
  1053. if (inet6_add_protocol(&sctpv6_protocol, IPPROTO_SCTP) < 0)
  1054. return -EAGAIN;
  1055. return 0;
  1056. }
  1057. /* Unregister with inet6 layer. */
  1058. void sctp_v6_del_protocol(void)
  1059. {
  1060. inet6_del_protocol(&sctpv6_protocol, IPPROTO_SCTP);
  1061. unregister_inet6addr_notifier(&sctp_inet6addr_notifier);
  1062. }