ipv6.c 32 KB

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