ipv6.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  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_flowinfo = 0;
  479. addr->v6.sin6_addr = sk->sk_v6_rcv_saddr;
  480. addr->v6.sin6_scope_id = 0;
  481. }
  482. /* Initialize sk->sk_rcv_saddr from sctp_addr. */
  483. static void sctp_v6_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
  484. {
  485. if (addr->sa.sa_family == AF_INET) {
  486. sk->sk_v6_rcv_saddr.s6_addr32[0] = 0;
  487. sk->sk_v6_rcv_saddr.s6_addr32[1] = 0;
  488. sk->sk_v6_rcv_saddr.s6_addr32[2] = htonl(0x0000ffff);
  489. sk->sk_v6_rcv_saddr.s6_addr32[3] =
  490. addr->v4.sin_addr.s_addr;
  491. } else {
  492. sk->sk_v6_rcv_saddr = addr->v6.sin6_addr;
  493. }
  494. }
  495. /* Initialize sk->sk_daddr from sctp_addr. */
  496. static void sctp_v6_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
  497. {
  498. if (addr->sa.sa_family == AF_INET) {
  499. sk->sk_v6_daddr.s6_addr32[0] = 0;
  500. sk->sk_v6_daddr.s6_addr32[1] = 0;
  501. sk->sk_v6_daddr.s6_addr32[2] = htonl(0x0000ffff);
  502. sk->sk_v6_daddr.s6_addr32[3] = addr->v4.sin_addr.s_addr;
  503. } else {
  504. sk->sk_v6_daddr = addr->v6.sin6_addr;
  505. }
  506. }
  507. /* Initialize a sctp_addr from an address parameter. */
  508. static bool sctp_v6_from_addr_param(union sctp_addr *addr,
  509. union sctp_addr_param *param,
  510. __be16 port, int iif)
  511. {
  512. if (ntohs(param->v6.param_hdr.length) < sizeof(struct sctp_ipv6addr_param))
  513. return false;
  514. addr->v6.sin6_family = AF_INET6;
  515. addr->v6.sin6_port = port;
  516. addr->v6.sin6_flowinfo = 0; /* BUG */
  517. addr->v6.sin6_addr = param->v6.addr;
  518. addr->v6.sin6_scope_id = iif;
  519. return true;
  520. }
  521. /* Initialize an address parameter from a sctp_addr and return the length
  522. * of the address parameter.
  523. */
  524. static int sctp_v6_to_addr_param(const union sctp_addr *addr,
  525. union sctp_addr_param *param)
  526. {
  527. int length = sizeof(struct sctp_ipv6addr_param);
  528. param->v6.param_hdr.type = SCTP_PARAM_IPV6_ADDRESS;
  529. param->v6.param_hdr.length = htons(length);
  530. param->v6.addr = addr->v6.sin6_addr;
  531. return length;
  532. }
  533. /* Initialize a sctp_addr from struct in6_addr. */
  534. static void sctp_v6_to_addr(union sctp_addr *addr, struct in6_addr *saddr,
  535. __be16 port)
  536. {
  537. addr->sa.sa_family = AF_INET6;
  538. addr->v6.sin6_port = port;
  539. addr->v6.sin6_flowinfo = 0;
  540. addr->v6.sin6_addr = *saddr;
  541. addr->v6.sin6_scope_id = 0;
  542. }
  543. static int __sctp_v6_cmp_addr(const union sctp_addr *addr1,
  544. const union sctp_addr *addr2)
  545. {
  546. if (addr1->sa.sa_family != addr2->sa.sa_family) {
  547. if (addr1->sa.sa_family == AF_INET &&
  548. addr2->sa.sa_family == AF_INET6 &&
  549. ipv6_addr_v4mapped(&addr2->v6.sin6_addr) &&
  550. addr2->v6.sin6_addr.s6_addr32[3] ==
  551. addr1->v4.sin_addr.s_addr)
  552. return 1;
  553. if (addr2->sa.sa_family == AF_INET &&
  554. addr1->sa.sa_family == AF_INET6 &&
  555. ipv6_addr_v4mapped(&addr1->v6.sin6_addr) &&
  556. addr1->v6.sin6_addr.s6_addr32[3] ==
  557. addr2->v4.sin_addr.s_addr)
  558. return 1;
  559. return 0;
  560. }
  561. if (!ipv6_addr_equal(&addr1->v6.sin6_addr, &addr2->v6.sin6_addr))
  562. return 0;
  563. /* If this is a linklocal address, compare the scope_id. */
  564. if ((ipv6_addr_type(&addr1->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) &&
  565. addr1->v6.sin6_scope_id && addr2->v6.sin6_scope_id &&
  566. addr1->v6.sin6_scope_id != addr2->v6.sin6_scope_id)
  567. return 0;
  568. return 1;
  569. }
  570. /* Compare addresses exactly.
  571. * v4-mapped-v6 is also in consideration.
  572. */
  573. static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
  574. const union sctp_addr *addr2)
  575. {
  576. return __sctp_v6_cmp_addr(addr1, addr2) &&
  577. addr1->v6.sin6_port == addr2->v6.sin6_port;
  578. }
  579. /* Initialize addr struct to INADDR_ANY. */
  580. static void sctp_v6_inaddr_any(union sctp_addr *addr, __be16 port)
  581. {
  582. memset(addr, 0x00, sizeof(union sctp_addr));
  583. addr->v6.sin6_family = AF_INET6;
  584. addr->v6.sin6_port = port;
  585. }
  586. /* Is this a wildcard address? */
  587. static int sctp_v6_is_any(const union sctp_addr *addr)
  588. {
  589. return ipv6_addr_any(&addr->v6.sin6_addr);
  590. }
  591. /* Should this be available for binding? */
  592. static int sctp_v6_available(union sctp_addr *addr, struct sctp_sock *sp)
  593. {
  594. const struct in6_addr *in6 = (const struct in6_addr *)&addr->v6.sin6_addr;
  595. struct sock *sk = &sp->inet.sk;
  596. struct net *net = sock_net(sk);
  597. struct net_device *dev = NULL;
  598. int type, res, bound_dev_if;
  599. type = ipv6_addr_type(in6);
  600. if (IPV6_ADDR_ANY == type)
  601. return 1;
  602. if (type == IPV6_ADDR_MAPPED) {
  603. if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
  604. return 0;
  605. sctp_v6_map_v4(addr);
  606. return sctp_get_af_specific(AF_INET)->available(addr, sp);
  607. }
  608. if (!(type & IPV6_ADDR_UNICAST))
  609. return 0;
  610. rcu_read_lock();
  611. bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
  612. if (bound_dev_if) {
  613. res = 0;
  614. dev = dev_get_by_index_rcu(net, bound_dev_if);
  615. if (!dev)
  616. goto out;
  617. }
  618. res = ipv6_can_nonlocal_bind(net, &sp->inet) ||
  619. ipv6_chk_addr(net, in6, dev, 0);
  620. out:
  621. rcu_read_unlock();
  622. return res;
  623. }
  624. /* This function checks if the address is a valid address to be used for
  625. * SCTP.
  626. *
  627. * Output:
  628. * Return 0 - If the address is a non-unicast or an illegal address.
  629. * Return 1 - If the address is a unicast.
  630. */
  631. static int sctp_v6_addr_valid(union sctp_addr *addr,
  632. struct sctp_sock *sp,
  633. const struct sk_buff *skb)
  634. {
  635. int ret = ipv6_addr_type(&addr->v6.sin6_addr);
  636. /* Support v4-mapped-v6 address. */
  637. if (ret == IPV6_ADDR_MAPPED) {
  638. /* Note: This routine is used in input, so v4-mapped-v6
  639. * are disallowed here when there is no sctp_sock.
  640. */
  641. if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
  642. return 0;
  643. sctp_v6_map_v4(addr);
  644. return sctp_get_af_specific(AF_INET)->addr_valid(addr, sp, skb);
  645. }
  646. /* Is this a non-unicast address */
  647. if (!(ret & IPV6_ADDR_UNICAST))
  648. return 0;
  649. return 1;
  650. }
  651. /* What is the scope of 'addr'? */
  652. static enum sctp_scope sctp_v6_scope(union sctp_addr *addr)
  653. {
  654. enum sctp_scope retval;
  655. int v6scope;
  656. /* The IPv6 scope is really a set of bit fields.
  657. * See IFA_* in <net/if_inet6.h>. Map to a generic SCTP scope.
  658. */
  659. v6scope = ipv6_addr_scope(&addr->v6.sin6_addr);
  660. switch (v6scope) {
  661. case IFA_HOST:
  662. retval = SCTP_SCOPE_LOOPBACK;
  663. break;
  664. case IFA_LINK:
  665. retval = SCTP_SCOPE_LINK;
  666. break;
  667. case IFA_SITE:
  668. retval = SCTP_SCOPE_PRIVATE;
  669. break;
  670. default:
  671. retval = SCTP_SCOPE_GLOBAL;
  672. break;
  673. }
  674. return retval;
  675. }
  676. /* Create and initialize a new sk for the socket to be returned by accept(). */
  677. static struct sock *sctp_v6_create_accept_sk(struct sock *sk,
  678. struct sctp_association *asoc,
  679. bool kern)
  680. {
  681. struct sock *newsk;
  682. struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
  683. struct sctp6_sock *newsctp6sk;
  684. newsk = sk_alloc(sock_net(sk), PF_INET6, GFP_KERNEL, sk->sk_prot, kern);
  685. if (!newsk)
  686. goto out;
  687. sock_init_data(NULL, newsk);
  688. sctp_copy_sock(newsk, sk, asoc);
  689. sock_reset_flag(sk, SOCK_ZAPPED);
  690. newsctp6sk = (struct sctp6_sock *)newsk;
  691. inet_sk(newsk)->pinet6 = &newsctp6sk->inet6;
  692. sctp_sk(newsk)->v4mapped = sctp_sk(sk)->v4mapped;
  693. newnp = inet6_sk(newsk);
  694. memcpy(newnp, np, sizeof(struct ipv6_pinfo));
  695. newnp->ipv6_mc_list = NULL;
  696. newnp->ipv6_ac_list = NULL;
  697. newnp->ipv6_fl_list = NULL;
  698. sctp_v6_copy_ip_options(sk, newsk);
  699. /* Initialize sk's sport, dport, rcv_saddr and daddr for getsockname()
  700. * and getpeername().
  701. */
  702. sctp_v6_to_sk_daddr(&asoc->peer.primary_addr, newsk);
  703. newsk->sk_v6_rcv_saddr = sk->sk_v6_rcv_saddr;
  704. if (newsk->sk_prot->init(newsk)) {
  705. sk_common_release(newsk);
  706. newsk = NULL;
  707. }
  708. out:
  709. return newsk;
  710. }
  711. /* Format a sockaddr for return to user space. This makes sure the return is
  712. * AF_INET or AF_INET6 depending on the SCTP_I_WANT_MAPPED_V4_ADDR option.
  713. */
  714. static int sctp_v6_addr_to_user(struct sctp_sock *sp, union sctp_addr *addr)
  715. {
  716. if (sp->v4mapped) {
  717. if (addr->sa.sa_family == AF_INET)
  718. sctp_v4_map_v6(addr);
  719. } else {
  720. if (addr->sa.sa_family == AF_INET6 &&
  721. ipv6_addr_v4mapped(&addr->v6.sin6_addr))
  722. sctp_v6_map_v4(addr);
  723. }
  724. if (addr->sa.sa_family == AF_INET) {
  725. memset(addr->v4.sin_zero, 0, sizeof(addr->v4.sin_zero));
  726. return sizeof(struct sockaddr_in);
  727. }
  728. return sizeof(struct sockaddr_in6);
  729. }
  730. /* Where did this skb come from? */
  731. static int sctp_v6_skb_iif(const struct sk_buff *skb)
  732. {
  733. return inet6_iif(skb);
  734. }
  735. static int sctp_v6_skb_sdif(const struct sk_buff *skb)
  736. {
  737. return inet6_sdif(skb);
  738. }
  739. /* Was this packet marked by Explicit Congestion Notification? */
  740. static int sctp_v6_is_ce(const struct sk_buff *skb)
  741. {
  742. return *((__u32 *)(ipv6_hdr(skb))) & (__force __u32)htonl(1 << 20);
  743. }
  744. /* Dump the v6 addr to the seq file. */
  745. static void sctp_v6_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
  746. {
  747. seq_printf(seq, "%pI6 ", &addr->v6.sin6_addr);
  748. }
  749. static void sctp_v6_ecn_capable(struct sock *sk)
  750. {
  751. inet6_sk(sk)->tclass |= INET_ECN_ECT_0;
  752. }
  753. /* Initialize a PF_INET msgname from a ulpevent. */
  754. static void sctp_inet6_event_msgname(struct sctp_ulpevent *event,
  755. char *msgname, int *addrlen)
  756. {
  757. union sctp_addr *addr;
  758. struct sctp_association *asoc;
  759. union sctp_addr *paddr;
  760. if (!msgname)
  761. return;
  762. addr = (union sctp_addr *)msgname;
  763. asoc = event->asoc;
  764. paddr = &asoc->peer.primary_addr;
  765. if (paddr->sa.sa_family == AF_INET) {
  766. addr->v4.sin_family = AF_INET;
  767. addr->v4.sin_port = htons(asoc->peer.port);
  768. addr->v4.sin_addr = paddr->v4.sin_addr;
  769. } else {
  770. addr->v6.sin6_family = AF_INET6;
  771. addr->v6.sin6_flowinfo = 0;
  772. if (ipv6_addr_type(&paddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
  773. addr->v6.sin6_scope_id = paddr->v6.sin6_scope_id;
  774. else
  775. addr->v6.sin6_scope_id = 0;
  776. addr->v6.sin6_port = htons(asoc->peer.port);
  777. addr->v6.sin6_addr = paddr->v6.sin6_addr;
  778. }
  779. *addrlen = sctp_v6_addr_to_user(sctp_sk(asoc->base.sk), addr);
  780. }
  781. /* Initialize a msg_name from an inbound skb. */
  782. static void sctp_inet6_skb_msgname(struct sk_buff *skb, char *msgname,
  783. int *addr_len)
  784. {
  785. union sctp_addr *addr;
  786. struct sctphdr *sh;
  787. if (!msgname)
  788. return;
  789. addr = (union sctp_addr *)msgname;
  790. sh = sctp_hdr(skb);
  791. if (ip_hdr(skb)->version == 4) {
  792. addr->v4.sin_family = AF_INET;
  793. addr->v4.sin_port = sh->source;
  794. addr->v4.sin_addr.s_addr = ip_hdr(skb)->saddr;
  795. } else {
  796. addr->v6.sin6_family = AF_INET6;
  797. addr->v6.sin6_flowinfo = 0;
  798. addr->v6.sin6_port = sh->source;
  799. addr->v6.sin6_addr = ipv6_hdr(skb)->saddr;
  800. if (ipv6_addr_type(&addr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
  801. addr->v6.sin6_scope_id = sctp_v6_skb_iif(skb);
  802. else
  803. addr->v6.sin6_scope_id = 0;
  804. }
  805. *addr_len = sctp_v6_addr_to_user(sctp_sk(skb->sk), addr);
  806. }
  807. /* Do we support this AF? */
  808. static int sctp_inet6_af_supported(sa_family_t family, struct sctp_sock *sp)
  809. {
  810. switch (family) {
  811. case AF_INET6:
  812. return 1;
  813. /* v4-mapped-v6 addresses */
  814. case AF_INET:
  815. if (!ipv6_only_sock(sctp_opt2sk(sp)))
  816. return 1;
  817. fallthrough;
  818. default:
  819. return 0;
  820. }
  821. }
  822. /* Address matching with wildcards allowed. This extra level
  823. * of indirection lets us choose whether a PF_INET6 should
  824. * disallow any v4 addresses if we so choose.
  825. */
  826. static int sctp_inet6_cmp_addr(const union sctp_addr *addr1,
  827. const union sctp_addr *addr2,
  828. struct sctp_sock *opt)
  829. {
  830. struct sock *sk = sctp_opt2sk(opt);
  831. struct sctp_af *af1, *af2;
  832. af1 = sctp_get_af_specific(addr1->sa.sa_family);
  833. af2 = sctp_get_af_specific(addr2->sa.sa_family);
  834. if (!af1 || !af2)
  835. return 0;
  836. /* If the socket is IPv6 only, v4 addrs will not match */
  837. if (ipv6_only_sock(sk) && af1 != af2)
  838. return 0;
  839. /* Today, wildcard AF_INET/AF_INET6. */
  840. if (sctp_is_any(sk, addr1) || sctp_is_any(sk, addr2))
  841. return 1;
  842. if (addr1->sa.sa_family == AF_INET && addr2->sa.sa_family == AF_INET)
  843. return addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr;
  844. return __sctp_v6_cmp_addr(addr1, addr2);
  845. }
  846. /* Verify that the provided sockaddr looks bindable. Common verification,
  847. * has already been taken care of.
  848. */
  849. static int sctp_inet6_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
  850. {
  851. struct sctp_af *af;
  852. /* ASSERT: address family has already been verified. */
  853. if (addr->sa.sa_family != AF_INET6)
  854. af = sctp_get_af_specific(addr->sa.sa_family);
  855. else {
  856. int type = ipv6_addr_type(&addr->v6.sin6_addr);
  857. struct net_device *dev;
  858. if (type & IPV6_ADDR_LINKLOCAL) {
  859. struct net *net;
  860. if (!addr->v6.sin6_scope_id)
  861. return 0;
  862. net = sock_net(&opt->inet.sk);
  863. rcu_read_lock();
  864. dev = dev_get_by_index_rcu(net, addr->v6.sin6_scope_id);
  865. if (!dev || !(ipv6_can_nonlocal_bind(net, &opt->inet) ||
  866. ipv6_chk_addr(net, &addr->v6.sin6_addr,
  867. dev, 0))) {
  868. rcu_read_unlock();
  869. return 0;
  870. }
  871. rcu_read_unlock();
  872. }
  873. af = opt->pf->af;
  874. }
  875. return af->available(addr, opt);
  876. }
  877. /* Verify that the provided sockaddr looks sendable. Common verification,
  878. * has already been taken care of.
  879. */
  880. static int sctp_inet6_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
  881. {
  882. struct sctp_af *af = NULL;
  883. /* ASSERT: address family has already been verified. */
  884. if (addr->sa.sa_family != AF_INET6)
  885. af = sctp_get_af_specific(addr->sa.sa_family);
  886. else {
  887. int type = ipv6_addr_type(&addr->v6.sin6_addr);
  888. struct net_device *dev;
  889. if (type & IPV6_ADDR_LINKLOCAL) {
  890. if (!addr->v6.sin6_scope_id)
  891. return 0;
  892. rcu_read_lock();
  893. dev = dev_get_by_index_rcu(sock_net(&opt->inet.sk),
  894. addr->v6.sin6_scope_id);
  895. rcu_read_unlock();
  896. if (!dev)
  897. return 0;
  898. }
  899. af = opt->pf->af;
  900. }
  901. return af != NULL;
  902. }
  903. /* Fill in Supported Address Type information for INIT and INIT-ACK
  904. * chunks. Note: In the future, we may want to look at sock options
  905. * to determine whether a PF_INET6 socket really wants to have IPV4
  906. * addresses.
  907. * Returns number of addresses supported.
  908. */
  909. static int sctp_inet6_supported_addrs(const struct sctp_sock *opt,
  910. __be16 *types)
  911. {
  912. types[0] = SCTP_PARAM_IPV6_ADDRESS;
  913. if (!opt || !ipv6_only_sock(sctp_opt2sk(opt))) {
  914. types[1] = SCTP_PARAM_IPV4_ADDRESS;
  915. return 2;
  916. }
  917. return 1;
  918. }
  919. /* Handle SCTP_I_WANT_MAPPED_V4_ADDR for getpeername() and getsockname() */
  920. static int sctp_getname(struct socket *sock, struct sockaddr *uaddr,
  921. int peer)
  922. {
  923. int rc;
  924. rc = inet6_getname(sock, uaddr, peer);
  925. if (rc < 0)
  926. return rc;
  927. rc = sctp_v6_addr_to_user(sctp_sk(sock->sk),
  928. (union sctp_addr *)uaddr);
  929. return rc;
  930. }
  931. static const struct proto_ops inet6_seqpacket_ops = {
  932. .family = PF_INET6,
  933. .owner = THIS_MODULE,
  934. .release = inet6_release,
  935. .bind = inet6_bind,
  936. .connect = sctp_inet_connect,
  937. .socketpair = sock_no_socketpair,
  938. .accept = inet_accept,
  939. .getname = sctp_getname,
  940. .poll = sctp_poll,
  941. .ioctl = inet6_ioctl,
  942. .gettstamp = sock_gettstamp,
  943. .listen = sctp_inet_listen,
  944. .shutdown = inet_shutdown,
  945. .setsockopt = sock_common_setsockopt,
  946. .getsockopt = sock_common_getsockopt,
  947. .sendmsg = inet_sendmsg,
  948. .recvmsg = inet_recvmsg,
  949. .mmap = sock_no_mmap,
  950. #ifdef CONFIG_COMPAT
  951. .compat_ioctl = inet6_compat_ioctl,
  952. #endif
  953. };
  954. static struct inet_protosw sctpv6_seqpacket_protosw = {
  955. .type = SOCK_SEQPACKET,
  956. .protocol = IPPROTO_SCTP,
  957. .prot = &sctpv6_prot,
  958. .ops = &inet6_seqpacket_ops,
  959. .flags = SCTP_PROTOSW_FLAG
  960. };
  961. static struct inet_protosw sctpv6_stream_protosw = {
  962. .type = SOCK_STREAM,
  963. .protocol = IPPROTO_SCTP,
  964. .prot = &sctpv6_prot,
  965. .ops = &inet6_seqpacket_ops,
  966. .flags = SCTP_PROTOSW_FLAG,
  967. };
  968. static int sctp6_rcv(struct sk_buff *skb)
  969. {
  970. SCTP_INPUT_CB(skb)->encap_port = 0;
  971. return sctp_rcv(skb) ? -1 : 0;
  972. }
  973. static const struct inet6_protocol sctpv6_protocol = {
  974. .handler = sctp6_rcv,
  975. .err_handler = sctp_v6_err,
  976. .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
  977. };
  978. static struct sctp_af sctp_af_inet6 = {
  979. .sa_family = AF_INET6,
  980. .sctp_xmit = sctp_v6_xmit,
  981. .setsockopt = ipv6_setsockopt,
  982. .getsockopt = ipv6_getsockopt,
  983. .get_dst = sctp_v6_get_dst,
  984. .get_saddr = sctp_v6_get_saddr,
  985. .copy_addrlist = sctp_v6_copy_addrlist,
  986. .from_skb = sctp_v6_from_skb,
  987. .from_sk = sctp_v6_from_sk,
  988. .from_addr_param = sctp_v6_from_addr_param,
  989. .to_addr_param = sctp_v6_to_addr_param,
  990. .cmp_addr = sctp_v6_cmp_addr,
  991. .scope = sctp_v6_scope,
  992. .addr_valid = sctp_v6_addr_valid,
  993. .inaddr_any = sctp_v6_inaddr_any,
  994. .is_any = sctp_v6_is_any,
  995. .available = sctp_v6_available,
  996. .skb_iif = sctp_v6_skb_iif,
  997. .skb_sdif = sctp_v6_skb_sdif,
  998. .is_ce = sctp_v6_is_ce,
  999. .seq_dump_addr = sctp_v6_seq_dump_addr,
  1000. .ecn_capable = sctp_v6_ecn_capable,
  1001. .net_header_len = sizeof(struct ipv6hdr),
  1002. .sockaddr_len = sizeof(struct sockaddr_in6),
  1003. .ip_options_len = sctp_v6_ip_options_len,
  1004. };
  1005. static struct sctp_pf sctp_pf_inet6 = {
  1006. .event_msgname = sctp_inet6_event_msgname,
  1007. .skb_msgname = sctp_inet6_skb_msgname,
  1008. .af_supported = sctp_inet6_af_supported,
  1009. .cmp_addr = sctp_inet6_cmp_addr,
  1010. .bind_verify = sctp_inet6_bind_verify,
  1011. .send_verify = sctp_inet6_send_verify,
  1012. .supported_addrs = sctp_inet6_supported_addrs,
  1013. .create_accept_sk = sctp_v6_create_accept_sk,
  1014. .addr_to_user = sctp_v6_addr_to_user,
  1015. .to_sk_saddr = sctp_v6_to_sk_saddr,
  1016. .to_sk_daddr = sctp_v6_to_sk_daddr,
  1017. .copy_ip_options = sctp_v6_copy_ip_options,
  1018. .af = &sctp_af_inet6,
  1019. };
  1020. /* Initialize IPv6 support and register with socket layer. */
  1021. void sctp_v6_pf_init(void)
  1022. {
  1023. /* Register the SCTP specific PF_INET6 functions. */
  1024. sctp_register_pf(&sctp_pf_inet6, PF_INET6);
  1025. /* Register the SCTP specific AF_INET6 functions. */
  1026. sctp_register_af(&sctp_af_inet6);
  1027. }
  1028. void sctp_v6_pf_exit(void)
  1029. {
  1030. list_del(&sctp_af_inet6.list);
  1031. }
  1032. /* Initialize IPv6 support and register with socket layer. */
  1033. int sctp_v6_protosw_init(void)
  1034. {
  1035. int rc;
  1036. rc = proto_register(&sctpv6_prot, 1);
  1037. if (rc)
  1038. return rc;
  1039. /* Add SCTPv6(UDP and TCP style) to inetsw6 linked list. */
  1040. inet6_register_protosw(&sctpv6_seqpacket_protosw);
  1041. inet6_register_protosw(&sctpv6_stream_protosw);
  1042. return 0;
  1043. }
  1044. void sctp_v6_protosw_exit(void)
  1045. {
  1046. inet6_unregister_protosw(&sctpv6_seqpacket_protosw);
  1047. inet6_unregister_protosw(&sctpv6_stream_protosw);
  1048. proto_unregister(&sctpv6_prot);
  1049. }
  1050. /* Register with inet6 layer. */
  1051. int sctp_v6_add_protocol(void)
  1052. {
  1053. /* Register notifier for inet6 address additions/deletions. */
  1054. register_inet6addr_notifier(&sctp_inet6addr_notifier);
  1055. if (inet6_add_protocol(&sctpv6_protocol, IPPROTO_SCTP) < 0)
  1056. return -EAGAIN;
  1057. return 0;
  1058. }
  1059. /* Unregister with inet6 layer. */
  1060. void sctp_v6_del_protocol(void)
  1061. {
  1062. inet6_del_protocol(&sctpv6_protocol, IPPROTO_SCTP);
  1063. unregister_inet6addr_notifier(&sctp_inet6addr_notifier);
  1064. }