ipv6.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. /* SCTP kernel reference Implementation
  2. * (C) Copyright IBM Corp. 2002, 2004
  3. * Copyright (c) 2001 Nokia, Inc.
  4. * Copyright (c) 2001 La Monte H.P. Yarroll
  5. * Copyright (c) 2002-2003 Intel Corp.
  6. *
  7. * This file is part of the SCTP kernel reference Implementation
  8. *
  9. * SCTP over IPv6.
  10. *
  11. * The SCTP reference implementation is free software;
  12. * you can redistribute it and/or modify it under the terms of
  13. * the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * The SCTP reference implementation is distributed in the hope that it
  18. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  19. * ************************
  20. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  21. * See the GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with GNU CC; see the file COPYING. If not, write to
  25. * the Free Software Foundation, 59 Temple Place - Suite 330,
  26. * Boston, MA 02111-1307, USA.
  27. *
  28. * Please send any bug reports or fixes you make to the
  29. * email address(es):
  30. * lksctp developers <lksctp-developers@lists.sourceforge.net>
  31. *
  32. * Or submit a bug report through the following website:
  33. * http://www.sf.net/projects/lksctp
  34. *
  35. * Written or modified by:
  36. * Le Yanqun <yanqun.le@nokia.com>
  37. * Hui Huang <hui.huang@nokia.com>
  38. * La Monte H.P. Yarroll <piggy@acm.org>
  39. * Sridhar Samudrala <sri@us.ibm.com>
  40. * Jon Grimm <jgrimm@us.ibm.com>
  41. * Ardelle Fan <ardelle.fan@intel.com>
  42. *
  43. * Based on:
  44. * linux/net/ipv6/tcp_ipv6.c
  45. *
  46. * Any bugs reported given to us we will try to fix... any fixes shared will
  47. * be incorporated into the next SCTP release.
  48. */
  49. #include <linux/module.h>
  50. #include <linux/errno.h>
  51. #include <linux/types.h>
  52. #include <linux/socket.h>
  53. #include <linux/sockios.h>
  54. #include <linux/net.h>
  55. #include <linux/sched.h>
  56. #include <linux/in.h>
  57. #include <linux/in6.h>
  58. #include <linux/netdevice.h>
  59. #include <linux/init.h>
  60. #include <linux/ipsec.h>
  61. #include <linux/ipv6.h>
  62. #include <linux/icmpv6.h>
  63. #include <linux/random.h>
  64. #include <linux/seq_file.h>
  65. #include <net/protocol.h>
  66. #include <net/tcp.h>
  67. #include <net/ndisc.h>
  68. #include <net/ipv6.h>
  69. #include <net/transp_v6.h>
  70. #include <net/addrconf.h>
  71. #include <net/ip6_route.h>
  72. #include <net/inet_common.h>
  73. #include <net/inet_ecn.h>
  74. #include <net/sctp/sctp.h>
  75. #include <asm/uaccess.h>
  76. extern int sctp_inetaddr_event(struct notifier_block *, unsigned long, void *);
  77. static struct notifier_block sctp_inet6addr_notifier = {
  78. .notifier_call = sctp_inetaddr_event,
  79. };
  80. /* ICMP error handler. */
  81. SCTP_STATIC void sctp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  82. int type, int code, int offset, __u32 info)
  83. {
  84. struct inet6_dev *idev;
  85. struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
  86. struct sctphdr *sh = (struct sctphdr *)(skb->data + offset);
  87. struct sock *sk;
  88. struct sctp_endpoint *ep;
  89. struct sctp_association *asoc;
  90. struct sctp_transport *transport;
  91. struct ipv6_pinfo *np;
  92. char *saveip, *savesctp;
  93. int err;
  94. idev = in6_dev_get(skb->dev);
  95. /* Fix up skb to look at the embedded net header. */
  96. saveip = skb->nh.raw;
  97. savesctp = skb->h.raw;
  98. skb->nh.ipv6h = iph;
  99. skb->h.raw = (char *)sh;
  100. sk = sctp_err_lookup(AF_INET6, skb, sh, &ep, &asoc, &transport);
  101. /* Put back, the original pointers. */
  102. skb->nh.raw = saveip;
  103. skb->h.raw = savesctp;
  104. if (!sk) {
  105. ICMP6_INC_STATS_BH(idev, ICMP6_MIB_INERRORS);
  106. goto out;
  107. }
  108. /* Warning: The sock lock is held. Remember to call
  109. * sctp_err_finish!
  110. */
  111. switch (type) {
  112. case ICMPV6_PKT_TOOBIG:
  113. sctp_icmp_frag_needed(sk, asoc, transport, ntohl(info));
  114. goto out_unlock;
  115. case ICMPV6_PARAMPROB:
  116. if (ICMPV6_UNK_NEXTHDR == code) {
  117. sctp_icmp_proto_unreachable(sk, ep, asoc, transport);
  118. goto out_unlock;
  119. }
  120. break;
  121. default:
  122. break;
  123. }
  124. np = inet6_sk(sk);
  125. icmpv6_err_convert(type, code, &err);
  126. if (!sock_owned_by_user(sk) && np->recverr) {
  127. sk->sk_err = err;
  128. sk->sk_error_report(sk);
  129. } else { /* Only an error on timeout */
  130. sk->sk_err_soft = err;
  131. }
  132. out_unlock:
  133. sctp_err_finish(sk, ep, asoc);
  134. out:
  135. if (likely(idev != NULL))
  136. in6_dev_put(idev);
  137. }
  138. /* Based on tcp_v6_xmit() in tcp_ipv6.c. */
  139. static int sctp_v6_xmit(struct sk_buff *skb, struct sctp_transport *transport,
  140. int ipfragok)
  141. {
  142. struct sock *sk = skb->sk;
  143. struct ipv6_pinfo *np = inet6_sk(sk);
  144. struct flowi fl;
  145. memset(&fl, 0, sizeof(fl));
  146. fl.proto = sk->sk_protocol;
  147. /* Fill in the dest address from the route entry passed with the skb
  148. * and the source address from the transport.
  149. */
  150. ipv6_addr_copy(&fl.fl6_dst, &transport->ipaddr.v6.sin6_addr);
  151. ipv6_addr_copy(&fl.fl6_src, &transport->saddr.v6.sin6_addr);
  152. fl.fl6_flowlabel = np->flow_label;
  153. IP6_ECN_flow_xmit(sk, fl.fl6_flowlabel);
  154. if (ipv6_addr_type(&fl.fl6_src) & IPV6_ADDR_LINKLOCAL)
  155. fl.oif = transport->saddr.v6.sin6_scope_id;
  156. else
  157. fl.oif = sk->sk_bound_dev_if;
  158. fl.fl_ip_sport = inet_sk(sk)->sport;
  159. fl.fl_ip_dport = transport->ipaddr.v6.sin6_port;
  160. if (np->opt && np->opt->srcrt) {
  161. struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
  162. ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
  163. }
  164. SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, "
  165. "src:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x "
  166. "dst:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
  167. __FUNCTION__, skb, skb->len,
  168. NIP6(fl.fl6_src), NIP6(fl.fl6_dst));
  169. SCTP_INC_STATS(SCTP_MIB_OUTSCTPPACKS);
  170. return ip6_xmit(sk, skb, &fl, np->opt, ipfragok);
  171. }
  172. /* Returns the dst cache entry for the given source and destination ip
  173. * addresses.
  174. */
  175. static struct dst_entry *sctp_v6_get_dst(struct sctp_association *asoc,
  176. union sctp_addr *daddr,
  177. union sctp_addr *saddr)
  178. {
  179. struct dst_entry *dst;
  180. struct flowi fl;
  181. memset(&fl, 0, sizeof(fl));
  182. ipv6_addr_copy(&fl.fl6_dst, &daddr->v6.sin6_addr);
  183. if (ipv6_addr_type(&daddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
  184. fl.oif = daddr->v6.sin6_scope_id;
  185. SCTP_DEBUG_PRINTK("%s: DST=%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ",
  186. __FUNCTION__, NIP6(fl.fl6_dst));
  187. if (saddr) {
  188. ipv6_addr_copy(&fl.fl6_src, &saddr->v6.sin6_addr);
  189. SCTP_DEBUG_PRINTK(
  190. "SRC=%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x - ",
  191. NIP6(fl.fl6_src));
  192. }
  193. dst = ip6_route_output(NULL, &fl);
  194. if (dst) {
  195. struct rt6_info *rt;
  196. rt = (struct rt6_info *)dst;
  197. SCTP_DEBUG_PRINTK(
  198. "rt6_dst:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x "
  199. "rt6_src:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
  200. NIP6(rt->rt6i_dst.addr), NIP6(rt->rt6i_src.addr));
  201. } else {
  202. SCTP_DEBUG_PRINTK("NO ROUTE\n");
  203. }
  204. return dst;
  205. }
  206. /* Returns the number of consecutive initial bits that match in the 2 ipv6
  207. * addresses.
  208. */
  209. static inline int sctp_v6_addr_match_len(union sctp_addr *s1,
  210. union sctp_addr *s2)
  211. {
  212. struct in6_addr *a1 = &s1->v6.sin6_addr;
  213. struct in6_addr *a2 = &s2->v6.sin6_addr;
  214. int i, j;
  215. for (i = 0; i < 4 ; i++) {
  216. __u32 a1xora2;
  217. a1xora2 = a1->s6_addr32[i] ^ a2->s6_addr32[i];
  218. if ((j = fls(ntohl(a1xora2))))
  219. return (i * 32 + 32 - j);
  220. }
  221. return (i*32);
  222. }
  223. /* Fills in the source address(saddr) based on the destination address(daddr)
  224. * and asoc's bind address list.
  225. */
  226. static void sctp_v6_get_saddr(struct sctp_association *asoc,
  227. struct dst_entry *dst,
  228. union sctp_addr *daddr,
  229. union sctp_addr *saddr)
  230. {
  231. struct sctp_bind_addr *bp;
  232. rwlock_t *addr_lock;
  233. struct sctp_sockaddr_entry *laddr;
  234. struct list_head *pos;
  235. sctp_scope_t scope;
  236. union sctp_addr *baddr = NULL;
  237. __u8 matchlen = 0;
  238. __u8 bmatchlen;
  239. SCTP_DEBUG_PRINTK("%s: asoc:%p dst:%p "
  240. "daddr:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ",
  241. __FUNCTION__, asoc, dst, NIP6(daddr->v6.sin6_addr));
  242. if (!asoc) {
  243. ipv6_get_saddr(dst, &daddr->v6.sin6_addr,&saddr->v6.sin6_addr);
  244. SCTP_DEBUG_PRINTK("saddr from ipv6_get_saddr: "
  245. "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
  246. NIP6(saddr->v6.sin6_addr));
  247. return;
  248. }
  249. scope = sctp_scope(daddr);
  250. bp = &asoc->base.bind_addr;
  251. addr_lock = &asoc->base.addr_lock;
  252. /* Go through the bind address list and find the best source address
  253. * that matches the scope of the destination address.
  254. */
  255. sctp_read_lock(addr_lock);
  256. list_for_each(pos, &bp->address_list) {
  257. laddr = list_entry(pos, struct sctp_sockaddr_entry, list);
  258. if ((laddr->a.sa.sa_family == AF_INET6) &&
  259. (scope <= sctp_scope(&laddr->a))) {
  260. bmatchlen = sctp_v6_addr_match_len(daddr, &laddr->a);
  261. if (!baddr || (matchlen < bmatchlen)) {
  262. baddr = &laddr->a;
  263. matchlen = bmatchlen;
  264. }
  265. }
  266. }
  267. if (baddr) {
  268. memcpy(saddr, baddr, sizeof(union sctp_addr));
  269. SCTP_DEBUG_PRINTK("saddr: "
  270. "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
  271. NIP6(saddr->v6.sin6_addr));
  272. } else {
  273. printk(KERN_ERR "%s: asoc:%p Could not find a valid source "
  274. "address for the "
  275. "dest:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
  276. __FUNCTION__, asoc, NIP6(daddr->v6.sin6_addr));
  277. }
  278. sctp_read_unlock(addr_lock);
  279. }
  280. /* Make a copy of all potential local addresses. */
  281. static void sctp_v6_copy_addrlist(struct list_head *addrlist,
  282. struct net_device *dev)
  283. {
  284. struct inet6_dev *in6_dev;
  285. struct inet6_ifaddr *ifp;
  286. struct sctp_sockaddr_entry *addr;
  287. read_lock(&addrconf_lock);
  288. if ((in6_dev = __in6_dev_get(dev)) == NULL) {
  289. read_unlock(&addrconf_lock);
  290. return;
  291. }
  292. read_lock(&in6_dev->lock);
  293. for (ifp = in6_dev->addr_list; ifp; ifp = ifp->if_next) {
  294. /* Add the address to the local list. */
  295. addr = t_new(struct sctp_sockaddr_entry, GFP_ATOMIC);
  296. if (addr) {
  297. addr->a.v6.sin6_family = AF_INET6;
  298. addr->a.v6.sin6_port = 0;
  299. addr->a.v6.sin6_addr = ifp->addr;
  300. addr->a.v6.sin6_scope_id = dev->ifindex;
  301. INIT_LIST_HEAD(&addr->list);
  302. list_add_tail(&addr->list, addrlist);
  303. }
  304. }
  305. read_unlock(&in6_dev->lock);
  306. read_unlock(&addrconf_lock);
  307. }
  308. /* Initialize a sockaddr_storage from in incoming skb. */
  309. static void sctp_v6_from_skb(union sctp_addr *addr,struct sk_buff *skb,
  310. int is_saddr)
  311. {
  312. void *from;
  313. __u16 *port;
  314. struct sctphdr *sh;
  315. port = &addr->v6.sin6_port;
  316. addr->v6.sin6_family = AF_INET6;
  317. addr->v6.sin6_flowinfo = 0; /* FIXME */
  318. addr->v6.sin6_scope_id = ((struct inet6_skb_parm *)skb->cb)->iif;
  319. sh = (struct sctphdr *) skb->h.raw;
  320. if (is_saddr) {
  321. *port = ntohs(sh->source);
  322. from = &skb->nh.ipv6h->saddr;
  323. } else {
  324. *port = ntohs(sh->dest);
  325. from = &skb->nh.ipv6h->daddr;
  326. }
  327. ipv6_addr_copy(&addr->v6.sin6_addr, from);
  328. }
  329. /* Initialize an sctp_addr from a socket. */
  330. static void sctp_v6_from_sk(union sctp_addr *addr, struct sock *sk)
  331. {
  332. addr->v6.sin6_family = AF_INET6;
  333. addr->v6.sin6_port = inet_sk(sk)->num;
  334. addr->v6.sin6_addr = inet6_sk(sk)->rcv_saddr;
  335. }
  336. /* Initialize sk->sk_rcv_saddr from sctp_addr. */
  337. static void sctp_v6_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
  338. {
  339. if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) {
  340. inet6_sk(sk)->rcv_saddr.s6_addr32[0] = 0;
  341. inet6_sk(sk)->rcv_saddr.s6_addr32[1] = 0;
  342. inet6_sk(sk)->rcv_saddr.s6_addr32[2] = htonl(0x0000ffff);
  343. inet6_sk(sk)->rcv_saddr.s6_addr32[3] =
  344. addr->v4.sin_addr.s_addr;
  345. } else {
  346. inet6_sk(sk)->rcv_saddr = addr->v6.sin6_addr;
  347. }
  348. }
  349. /* Initialize sk->sk_daddr from sctp_addr. */
  350. static void sctp_v6_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
  351. {
  352. if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) {
  353. inet6_sk(sk)->daddr.s6_addr32[0] = 0;
  354. inet6_sk(sk)->daddr.s6_addr32[1] = 0;
  355. inet6_sk(sk)->daddr.s6_addr32[2] = htonl(0x0000ffff);
  356. inet6_sk(sk)->daddr.s6_addr32[3] = addr->v4.sin_addr.s_addr;
  357. } else {
  358. inet6_sk(sk)->daddr = addr->v6.sin6_addr;
  359. }
  360. }
  361. /* Initialize a sctp_addr from an address parameter. */
  362. static void sctp_v6_from_addr_param(union sctp_addr *addr,
  363. union sctp_addr_param *param,
  364. __u16 port, int iif)
  365. {
  366. addr->v6.sin6_family = AF_INET6;
  367. addr->v6.sin6_port = port;
  368. addr->v6.sin6_flowinfo = 0; /* BUG */
  369. ipv6_addr_copy(&addr->v6.sin6_addr, &param->v6.addr);
  370. addr->v6.sin6_scope_id = iif;
  371. }
  372. /* Initialize an address parameter from a sctp_addr and return the length
  373. * of the address parameter.
  374. */
  375. static int sctp_v6_to_addr_param(const union sctp_addr *addr,
  376. union sctp_addr_param *param)
  377. {
  378. int length = sizeof(sctp_ipv6addr_param_t);
  379. param->v6.param_hdr.type = SCTP_PARAM_IPV6_ADDRESS;
  380. param->v6.param_hdr.length = ntohs(length);
  381. ipv6_addr_copy(&param->v6.addr, &addr->v6.sin6_addr);
  382. return length;
  383. }
  384. /* Initialize a sctp_addr from a dst_entry. */
  385. static void sctp_v6_dst_saddr(union sctp_addr *addr, struct dst_entry *dst,
  386. unsigned short port)
  387. {
  388. struct rt6_info *rt = (struct rt6_info *)dst;
  389. addr->sa.sa_family = AF_INET6;
  390. addr->v6.sin6_port = port;
  391. ipv6_addr_copy(&addr->v6.sin6_addr, &rt->rt6i_src.addr);
  392. }
  393. /* Compare addresses exactly.
  394. * v4-mapped-v6 is also in consideration.
  395. */
  396. static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
  397. const union sctp_addr *addr2)
  398. {
  399. if (addr1->sa.sa_family != addr2->sa.sa_family) {
  400. if (addr1->sa.sa_family == AF_INET &&
  401. addr2->sa.sa_family == AF_INET6 &&
  402. IPV6_ADDR_MAPPED == ipv6_addr_type(&addr2->v6.sin6_addr)) {
  403. if (addr2->v6.sin6_port == addr1->v4.sin_port &&
  404. addr2->v6.sin6_addr.s6_addr32[3] ==
  405. addr1->v4.sin_addr.s_addr)
  406. return 1;
  407. }
  408. if (addr2->sa.sa_family == AF_INET &&
  409. addr1->sa.sa_family == AF_INET6 &&
  410. IPV6_ADDR_MAPPED == ipv6_addr_type(&addr1->v6.sin6_addr)) {
  411. if (addr1->v6.sin6_port == addr2->v4.sin_port &&
  412. addr1->v6.sin6_addr.s6_addr32[3] ==
  413. addr2->v4.sin_addr.s_addr)
  414. return 1;
  415. }
  416. return 0;
  417. }
  418. if (!ipv6_addr_equal(&addr1->v6.sin6_addr, &addr2->v6.sin6_addr))
  419. return 0;
  420. /* If this is a linklocal address, compare the scope_id. */
  421. if (ipv6_addr_type(&addr1->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) {
  422. if (addr1->v6.sin6_scope_id && addr2->v6.sin6_scope_id &&
  423. (addr1->v6.sin6_scope_id != addr2->v6.sin6_scope_id)) {
  424. return 0;
  425. }
  426. }
  427. return 1;
  428. }
  429. /* Initialize addr struct to INADDR_ANY. */
  430. static void sctp_v6_inaddr_any(union sctp_addr *addr, unsigned short port)
  431. {
  432. memset(addr, 0x00, sizeof(union sctp_addr));
  433. addr->v6.sin6_family = AF_INET6;
  434. addr->v6.sin6_port = port;
  435. }
  436. /* Is this a wildcard address? */
  437. static int sctp_v6_is_any(const union sctp_addr *addr)
  438. {
  439. int type;
  440. type = ipv6_addr_type((struct in6_addr *)&addr->v6.sin6_addr);
  441. return IPV6_ADDR_ANY == type;
  442. }
  443. /* Should this be available for binding? */
  444. static int sctp_v6_available(union sctp_addr *addr, struct sctp_sock *sp)
  445. {
  446. int type;
  447. struct in6_addr *in6 = (struct in6_addr *)&addr->v6.sin6_addr;
  448. type = ipv6_addr_type(in6);
  449. if (IPV6_ADDR_ANY == type)
  450. return 1;
  451. if (type == IPV6_ADDR_MAPPED) {
  452. if (sp && !sp->v4mapped)
  453. return 0;
  454. if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
  455. return 0;
  456. sctp_v6_map_v4(addr);
  457. return sctp_get_af_specific(AF_INET)->available(addr, sp);
  458. }
  459. if (!(type & IPV6_ADDR_UNICAST))
  460. return 0;
  461. return ipv6_chk_addr(in6, NULL, 0);
  462. }
  463. /* This function checks if the address is a valid address to be used for
  464. * SCTP.
  465. *
  466. * Output:
  467. * Return 0 - If the address is a non-unicast or an illegal address.
  468. * Return 1 - If the address is a unicast.
  469. */
  470. static int sctp_v6_addr_valid(union sctp_addr *addr, struct sctp_sock *sp)
  471. {
  472. int ret = ipv6_addr_type(&addr->v6.sin6_addr);
  473. /* Support v4-mapped-v6 address. */
  474. if (ret == IPV6_ADDR_MAPPED) {
  475. /* Note: This routine is used in input, so v4-mapped-v6
  476. * are disallowed here when there is no sctp_sock.
  477. */
  478. if (!sp || !sp->v4mapped)
  479. return 0;
  480. if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
  481. return 0;
  482. sctp_v6_map_v4(addr);
  483. return sctp_get_af_specific(AF_INET)->addr_valid(addr, sp);
  484. }
  485. /* Is this a non-unicast address */
  486. if (!(ret & IPV6_ADDR_UNICAST))
  487. return 0;
  488. return 1;
  489. }
  490. /* What is the scope of 'addr'? */
  491. static sctp_scope_t sctp_v6_scope(union sctp_addr *addr)
  492. {
  493. int v6scope;
  494. sctp_scope_t retval;
  495. /* The IPv6 scope is really a set of bit fields.
  496. * See IFA_* in <net/if_inet6.h>. Map to a generic SCTP scope.
  497. */
  498. v6scope = ipv6_addr_scope(&addr->v6.sin6_addr);
  499. switch (v6scope) {
  500. case IFA_HOST:
  501. retval = SCTP_SCOPE_LOOPBACK;
  502. break;
  503. case IFA_LINK:
  504. retval = SCTP_SCOPE_LINK;
  505. break;
  506. case IFA_SITE:
  507. retval = SCTP_SCOPE_PRIVATE;
  508. break;
  509. default:
  510. retval = SCTP_SCOPE_GLOBAL;
  511. break;
  512. };
  513. return retval;
  514. }
  515. /* Create and initialize a new sk for the socket to be returned by accept(). */
  516. static struct sock *sctp_v6_create_accept_sk(struct sock *sk,
  517. struct sctp_association *asoc)
  518. {
  519. struct inet_sock *inet = inet_sk(sk);
  520. struct sock *newsk;
  521. struct inet_sock *newinet;
  522. struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
  523. struct sctp6_sock *newsctp6sk;
  524. newsk = sk_alloc(PF_INET6, GFP_KERNEL, sk->sk_prot, 1);
  525. if (!newsk)
  526. goto out;
  527. sock_init_data(NULL, newsk);
  528. newsk->sk_type = SOCK_STREAM;
  529. newsk->sk_prot = sk->sk_prot;
  530. newsk->sk_no_check = sk->sk_no_check;
  531. newsk->sk_reuse = sk->sk_reuse;
  532. newsk->sk_destruct = inet_sock_destruct;
  533. newsk->sk_family = PF_INET6;
  534. newsk->sk_protocol = IPPROTO_SCTP;
  535. newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
  536. newsk->sk_shutdown = sk->sk_shutdown;
  537. sock_reset_flag(sk, SOCK_ZAPPED);
  538. newsctp6sk = (struct sctp6_sock *)newsk;
  539. inet_sk(newsk)->pinet6 = &newsctp6sk->inet6;
  540. newinet = inet_sk(newsk);
  541. newnp = inet6_sk(newsk);
  542. memcpy(newnp, np, sizeof(struct ipv6_pinfo));
  543. /* Initialize sk's sport, dport, rcv_saddr and daddr for getsockname()
  544. * and getpeername().
  545. */
  546. newinet->sport = inet->sport;
  547. newnp->saddr = np->saddr;
  548. newnp->rcv_saddr = np->rcv_saddr;
  549. newinet->dport = htons(asoc->peer.port);
  550. sctp_v6_to_sk_daddr(&asoc->peer.primary_addr, newsk);
  551. /* Init the ipv4 part of the socket since we can have sockets
  552. * using v6 API for ipv4.
  553. */
  554. newinet->uc_ttl = -1;
  555. newinet->mc_loop = 1;
  556. newinet->mc_ttl = 1;
  557. newinet->mc_index = 0;
  558. newinet->mc_list = NULL;
  559. if (ipv4_config.no_pmtu_disc)
  560. newinet->pmtudisc = IP_PMTUDISC_DONT;
  561. else
  562. newinet->pmtudisc = IP_PMTUDISC_WANT;
  563. #ifdef INET_REFCNT_DEBUG
  564. atomic_inc(&inet6_sock_nr);
  565. atomic_inc(&inet_sock_nr);
  566. #endif
  567. if (newsk->sk_prot->init(newsk)) {
  568. sk_common_release(newsk);
  569. newsk = NULL;
  570. }
  571. out:
  572. return newsk;
  573. }
  574. /* Map v4 address to mapped v6 address */
  575. static void sctp_v6_addr_v4map(struct sctp_sock *sp, union sctp_addr *addr)
  576. {
  577. if (sp->v4mapped && AF_INET == addr->sa.sa_family)
  578. sctp_v4_map_v6(addr);
  579. }
  580. /* Where did this skb come from? */
  581. static int sctp_v6_skb_iif(const struct sk_buff *skb)
  582. {
  583. struct inet6_skb_parm *opt = (struct inet6_skb_parm *) skb->cb;
  584. return opt->iif;
  585. }
  586. /* Was this packet marked by Explicit Congestion Notification? */
  587. static int sctp_v6_is_ce(const struct sk_buff *skb)
  588. {
  589. return *((__u32 *)(skb->nh.ipv6h)) & htonl(1<<20);
  590. }
  591. /* Dump the v6 addr to the seq file. */
  592. static void sctp_v6_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
  593. {
  594. seq_printf(seq, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ",
  595. NIP6(addr->v6.sin6_addr));
  596. }
  597. /* Initialize a PF_INET6 socket msg_name. */
  598. static void sctp_inet6_msgname(char *msgname, int *addr_len)
  599. {
  600. struct sockaddr_in6 *sin6;
  601. sin6 = (struct sockaddr_in6 *)msgname;
  602. sin6->sin6_family = AF_INET6;
  603. sin6->sin6_flowinfo = 0;
  604. sin6->sin6_scope_id = 0; /*FIXME */
  605. *addr_len = sizeof(struct sockaddr_in6);
  606. }
  607. /* Initialize a PF_INET msgname from a ulpevent. */
  608. static void sctp_inet6_event_msgname(struct sctp_ulpevent *event,
  609. char *msgname, int *addrlen)
  610. {
  611. struct sockaddr_in6 *sin6, *sin6from;
  612. if (msgname) {
  613. union sctp_addr *addr;
  614. struct sctp_association *asoc;
  615. asoc = event->asoc;
  616. sctp_inet6_msgname(msgname, addrlen);
  617. sin6 = (struct sockaddr_in6 *)msgname;
  618. sin6->sin6_port = htons(asoc->peer.port);
  619. addr = &asoc->peer.primary_addr;
  620. /* Note: If we go to a common v6 format, this code
  621. * will change.
  622. */
  623. /* Map ipv4 address into v4-mapped-on-v6 address. */
  624. if (sctp_sk(asoc->base.sk)->v4mapped &&
  625. AF_INET == addr->sa.sa_family) {
  626. sctp_v4_map_v6((union sctp_addr *)sin6);
  627. sin6->sin6_addr.s6_addr32[3] =
  628. addr->v4.sin_addr.s_addr;
  629. return;
  630. }
  631. sin6from = &asoc->peer.primary_addr.v6;
  632. ipv6_addr_copy(&sin6->sin6_addr, &sin6from->sin6_addr);
  633. if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
  634. sin6->sin6_scope_id = sin6from->sin6_scope_id;
  635. }
  636. }
  637. /* Initialize a msg_name from an inbound skb. */
  638. static void sctp_inet6_skb_msgname(struct sk_buff *skb, char *msgname,
  639. int *addr_len)
  640. {
  641. struct sctphdr *sh;
  642. struct sockaddr_in6 *sin6;
  643. if (msgname) {
  644. sctp_inet6_msgname(msgname, addr_len);
  645. sin6 = (struct sockaddr_in6 *)msgname;
  646. sh = (struct sctphdr *)skb->h.raw;
  647. sin6->sin6_port = sh->source;
  648. /* Map ipv4 address into v4-mapped-on-v6 address. */
  649. if (sctp_sk(skb->sk)->v4mapped &&
  650. skb->nh.iph->version == 4) {
  651. sctp_v4_map_v6((union sctp_addr *)sin6);
  652. sin6->sin6_addr.s6_addr32[3] = skb->nh.iph->saddr;
  653. return;
  654. }
  655. /* Otherwise, just copy the v6 address. */
  656. ipv6_addr_copy(&sin6->sin6_addr, &skb->nh.ipv6h->saddr);
  657. if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) {
  658. struct sctp_ulpevent *ev = sctp_skb2event(skb);
  659. sin6->sin6_scope_id = ev->iif;
  660. }
  661. }
  662. }
  663. /* Do we support this AF? */
  664. static int sctp_inet6_af_supported(sa_family_t family, struct sctp_sock *sp)
  665. {
  666. switch (family) {
  667. case AF_INET6:
  668. return 1;
  669. /* v4-mapped-v6 addresses */
  670. case AF_INET:
  671. if (!__ipv6_only_sock(sctp_opt2sk(sp)) && sp->v4mapped)
  672. return 1;
  673. default:
  674. return 0;
  675. }
  676. }
  677. /* Address matching with wildcards allowed. This extra level
  678. * of indirection lets us choose whether a PF_INET6 should
  679. * disallow any v4 addresses if we so choose.
  680. */
  681. static int sctp_inet6_cmp_addr(const union sctp_addr *addr1,
  682. const union sctp_addr *addr2,
  683. struct sctp_sock *opt)
  684. {
  685. struct sctp_af *af1, *af2;
  686. af1 = sctp_get_af_specific(addr1->sa.sa_family);
  687. af2 = sctp_get_af_specific(addr2->sa.sa_family);
  688. if (!af1 || !af2)
  689. return 0;
  690. /* Today, wildcard AF_INET/AF_INET6. */
  691. if (sctp_is_any(addr1) || sctp_is_any(addr2))
  692. return 1;
  693. if (addr1->sa.sa_family != addr2->sa.sa_family)
  694. return 0;
  695. return af1->cmp_addr(addr1, addr2);
  696. }
  697. /* Verify that the provided sockaddr looks bindable. Common verification,
  698. * has already been taken care of.
  699. */
  700. static int sctp_inet6_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
  701. {
  702. struct sctp_af *af;
  703. /* ASSERT: address family has already been verified. */
  704. if (addr->sa.sa_family != AF_INET6)
  705. af = sctp_get_af_specific(addr->sa.sa_family);
  706. else {
  707. struct sock *sk;
  708. int type = ipv6_addr_type(&addr->v6.sin6_addr);
  709. sk = sctp_opt2sk(opt);
  710. if (type & IPV6_ADDR_LINKLOCAL) {
  711. /* Note: Behavior similar to af_inet6.c:
  712. * 1) Overrides previous bound_dev_if
  713. * 2) Destructive even if bind isn't successful.
  714. */
  715. if (addr->v6.sin6_scope_id)
  716. sk->sk_bound_dev_if = addr->v6.sin6_scope_id;
  717. if (!sk->sk_bound_dev_if)
  718. return 0;
  719. }
  720. af = opt->pf->af;
  721. }
  722. return af->available(addr, opt);
  723. }
  724. /* Verify that the provided sockaddr looks bindable. Common verification,
  725. * has already been taken care of.
  726. */
  727. static int sctp_inet6_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
  728. {
  729. struct sctp_af *af = NULL;
  730. /* ASSERT: address family has already been verified. */
  731. if (addr->sa.sa_family != AF_INET6)
  732. af = sctp_get_af_specific(addr->sa.sa_family);
  733. else {
  734. struct sock *sk;
  735. int type = ipv6_addr_type(&addr->v6.sin6_addr);
  736. sk = sctp_opt2sk(opt);
  737. if (type & IPV6_ADDR_LINKLOCAL) {
  738. /* Note: Behavior similar to af_inet6.c:
  739. * 1) Overrides previous bound_dev_if
  740. * 2) Destructive even if bind isn't successful.
  741. */
  742. if (addr->v6.sin6_scope_id)
  743. sk->sk_bound_dev_if = addr->v6.sin6_scope_id;
  744. if (!sk->sk_bound_dev_if)
  745. return 0;
  746. }
  747. af = opt->pf->af;
  748. }
  749. return af != NULL;
  750. }
  751. /* Fill in Supported Address Type information for INIT and INIT-ACK
  752. * chunks. Note: In the future, we may want to look at sock options
  753. * to determine whether a PF_INET6 socket really wants to have IPV4
  754. * addresses.
  755. * Returns number of addresses supported.
  756. */
  757. static int sctp_inet6_supported_addrs(const struct sctp_sock *opt,
  758. __u16 *types)
  759. {
  760. types[0] = SCTP_PARAM_IPV4_ADDRESS;
  761. types[1] = SCTP_PARAM_IPV6_ADDRESS;
  762. return 2;
  763. }
  764. static struct proto_ops inet6_seqpacket_ops = {
  765. .family = PF_INET6,
  766. .owner = THIS_MODULE,
  767. .release = inet6_release,
  768. .bind = inet6_bind,
  769. .connect = inet_dgram_connect,
  770. .socketpair = sock_no_socketpair,
  771. .accept = inet_accept,
  772. .getname = inet6_getname,
  773. .poll = sctp_poll,
  774. .ioctl = inet6_ioctl,
  775. .listen = sctp_inet_listen,
  776. .shutdown = inet_shutdown,
  777. .setsockopt = sock_common_setsockopt,
  778. .getsockopt = sock_common_getsockopt,
  779. .sendmsg = inet_sendmsg,
  780. .recvmsg = sock_common_recvmsg,
  781. .mmap = sock_no_mmap,
  782. };
  783. static struct inet_protosw sctpv6_seqpacket_protosw = {
  784. .type = SOCK_SEQPACKET,
  785. .protocol = IPPROTO_SCTP,
  786. .prot = &sctpv6_prot,
  787. .ops = &inet6_seqpacket_ops,
  788. .capability = -1,
  789. .no_check = 0,
  790. .flags = SCTP_PROTOSW_FLAG
  791. };
  792. static struct inet_protosw sctpv6_stream_protosw = {
  793. .type = SOCK_STREAM,
  794. .protocol = IPPROTO_SCTP,
  795. .prot = &sctpv6_prot,
  796. .ops = &inet6_seqpacket_ops,
  797. .capability = -1,
  798. .no_check = 0,
  799. .flags = SCTP_PROTOSW_FLAG,
  800. };
  801. static int sctp6_rcv(struct sk_buff **pskb, unsigned int *nhoffp)
  802. {
  803. return sctp_rcv(*pskb) ? -1 : 0;
  804. }
  805. static struct inet6_protocol sctpv6_protocol = {
  806. .handler = sctp6_rcv,
  807. .err_handler = sctp_v6_err,
  808. .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
  809. };
  810. static struct sctp_af sctp_ipv6_specific = {
  811. .sctp_xmit = sctp_v6_xmit,
  812. .setsockopt = ipv6_setsockopt,
  813. .getsockopt = ipv6_getsockopt,
  814. .get_dst = sctp_v6_get_dst,
  815. .get_saddr = sctp_v6_get_saddr,
  816. .copy_addrlist = sctp_v6_copy_addrlist,
  817. .from_skb = sctp_v6_from_skb,
  818. .from_sk = sctp_v6_from_sk,
  819. .to_sk_saddr = sctp_v6_to_sk_saddr,
  820. .to_sk_daddr = sctp_v6_to_sk_daddr,
  821. .from_addr_param = sctp_v6_from_addr_param,
  822. .to_addr_param = sctp_v6_to_addr_param,
  823. .dst_saddr = sctp_v6_dst_saddr,
  824. .cmp_addr = sctp_v6_cmp_addr,
  825. .scope = sctp_v6_scope,
  826. .addr_valid = sctp_v6_addr_valid,
  827. .inaddr_any = sctp_v6_inaddr_any,
  828. .is_any = sctp_v6_is_any,
  829. .available = sctp_v6_available,
  830. .skb_iif = sctp_v6_skb_iif,
  831. .is_ce = sctp_v6_is_ce,
  832. .seq_dump_addr = sctp_v6_seq_dump_addr,
  833. .net_header_len = sizeof(struct ipv6hdr),
  834. .sockaddr_len = sizeof(struct sockaddr_in6),
  835. .sa_family = AF_INET6,
  836. };
  837. static struct sctp_pf sctp_pf_inet6_specific = {
  838. .event_msgname = sctp_inet6_event_msgname,
  839. .skb_msgname = sctp_inet6_skb_msgname,
  840. .af_supported = sctp_inet6_af_supported,
  841. .cmp_addr = sctp_inet6_cmp_addr,
  842. .bind_verify = sctp_inet6_bind_verify,
  843. .send_verify = sctp_inet6_send_verify,
  844. .supported_addrs = sctp_inet6_supported_addrs,
  845. .create_accept_sk = sctp_v6_create_accept_sk,
  846. .addr_v4map = sctp_v6_addr_v4map,
  847. .af = &sctp_ipv6_specific,
  848. };
  849. /* Initialize IPv6 support and register with inet6 stack. */
  850. int sctp_v6_init(void)
  851. {
  852. int rc = proto_register(&sctpv6_prot, 1);
  853. if (rc)
  854. goto out;
  855. /* Register inet6 protocol. */
  856. rc = -EAGAIN;
  857. if (inet6_add_protocol(&sctpv6_protocol, IPPROTO_SCTP) < 0)
  858. goto out_unregister_sctp_proto;
  859. /* Add SCTPv6(UDP and TCP style) to inetsw6 linked list. */
  860. inet6_register_protosw(&sctpv6_seqpacket_protosw);
  861. inet6_register_protosw(&sctpv6_stream_protosw);
  862. /* Register the SCTP specific PF_INET6 functions. */
  863. sctp_register_pf(&sctp_pf_inet6_specific, PF_INET6);
  864. /* Register the SCTP specific AF_INET6 functions. */
  865. sctp_register_af(&sctp_ipv6_specific);
  866. /* Register notifier for inet6 address additions/deletions. */
  867. register_inet6addr_notifier(&sctp_inet6addr_notifier);
  868. rc = 0;
  869. out:
  870. return rc;
  871. out_unregister_sctp_proto:
  872. proto_unregister(&sctpv6_prot);
  873. goto out;
  874. }
  875. /* IPv6 specific exit support. */
  876. void sctp_v6_exit(void)
  877. {
  878. list_del(&sctp_ipv6_specific.list);
  879. inet6_del_protocol(&sctpv6_protocol, IPPROTO_SCTP);
  880. inet6_unregister_protosw(&sctpv6_seqpacket_protosw);
  881. inet6_unregister_protosw(&sctpv6_stream_protosw);
  882. unregister_inet6addr_notifier(&sctp_inet6addr_notifier);
  883. proto_unregister(&sctpv6_prot);
  884. }