af_mpls.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748
  1. #include <linux/types.h>
  2. #include <linux/skbuff.h>
  3. #include <linux/socket.h>
  4. #include <linux/sysctl.h>
  5. #include <linux/net.h>
  6. #include <linux/module.h>
  7. #include <linux/if_arp.h>
  8. #include <linux/ipv6.h>
  9. #include <linux/mpls.h>
  10. #include <linux/vmalloc.h>
  11. #include <net/ip.h>
  12. #include <net/dst.h>
  13. #include <net/sock.h>
  14. #include <net/arp.h>
  15. #include <net/ip_fib.h>
  16. #include <net/netevent.h>
  17. #include <net/netns/generic.h>
  18. #if IS_ENABLED(CONFIG_IPV6)
  19. #include <net/ipv6.h>
  20. #include <net/addrconf.h>
  21. #endif
  22. #include <net/nexthop.h>
  23. #include "internal.h"
  24. /* Maximum number of labels to look ahead at when selecting a path of
  25. * a multipath route
  26. */
  27. #define MAX_MP_SELECT_LABELS 4
  28. #define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1)
  29. static int zero = 0;
  30. static int label_limit = (1 << 20) - 1;
  31. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  32. struct nlmsghdr *nlh, struct net *net, u32 portid,
  33. unsigned int nlm_flags);
  34. static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
  35. {
  36. struct mpls_route *rt = NULL;
  37. if (index < net->mpls.platform_labels) {
  38. struct mpls_route __rcu **platform_label =
  39. rcu_dereference(net->mpls.platform_label);
  40. rt = rcu_dereference(platform_label[index]);
  41. }
  42. return rt;
  43. }
  44. static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev)
  45. {
  46. return rcu_dereference_rtnl(dev->mpls_ptr);
  47. }
  48. bool mpls_output_possible(const struct net_device *dev)
  49. {
  50. return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
  51. }
  52. EXPORT_SYMBOL_GPL(mpls_output_possible);
  53. static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh)
  54. {
  55. u8 *nh0_via = PTR_ALIGN((u8 *)&rt->rt_nh[rt->rt_nhn], VIA_ALEN_ALIGN);
  56. int nh_index = nh - rt->rt_nh;
  57. return nh0_via + rt->rt_max_alen * nh_index;
  58. }
  59. static const u8 *mpls_nh_via(const struct mpls_route *rt,
  60. const struct mpls_nh *nh)
  61. {
  62. return __mpls_nh_via((struct mpls_route *)rt, (struct mpls_nh *)nh);
  63. }
  64. static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
  65. {
  66. /* The size of the layer 2.5 labels to be added for this route */
  67. return nh->nh_labels * sizeof(struct mpls_shim_hdr);
  68. }
  69. unsigned int mpls_dev_mtu(const struct net_device *dev)
  70. {
  71. /* The amount of data the layer 2 frame can hold */
  72. return dev->mtu;
  73. }
  74. EXPORT_SYMBOL_GPL(mpls_dev_mtu);
  75. bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
  76. {
  77. if (skb->len <= mtu)
  78. return false;
  79. if (skb_is_gso(skb) && skb_gso_network_seglen(skb) <= mtu)
  80. return false;
  81. return true;
  82. }
  83. EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
  84. static u32 mpls_multipath_hash(struct mpls_route *rt,
  85. struct sk_buff *skb, bool bos)
  86. {
  87. struct mpls_entry_decoded dec;
  88. struct mpls_shim_hdr *hdr;
  89. bool eli_seen = false;
  90. int label_index;
  91. u32 hash = 0;
  92. for (label_index = 0; label_index < MAX_MP_SELECT_LABELS && !bos;
  93. label_index++) {
  94. if (!pskb_may_pull(skb, sizeof(*hdr) * label_index))
  95. break;
  96. /* Read and decode the current label */
  97. hdr = mpls_hdr(skb) + label_index;
  98. dec = mpls_entry_decode(hdr);
  99. /* RFC6790 - reserved labels MUST NOT be used as keys
  100. * for the load-balancing function
  101. */
  102. if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
  103. hash = jhash_1word(dec.label, hash);
  104. /* The entropy label follows the entropy label
  105. * indicator, so this means that the entropy
  106. * label was just added to the hash - no need to
  107. * go any deeper either in the label stack or in the
  108. * payload
  109. */
  110. if (eli_seen)
  111. break;
  112. } else if (dec.label == MPLS_LABEL_ENTROPY) {
  113. eli_seen = true;
  114. }
  115. bos = dec.bos;
  116. if (bos && pskb_may_pull(skb, sizeof(*hdr) * label_index +
  117. sizeof(struct iphdr))) {
  118. const struct iphdr *v4hdr;
  119. v4hdr = (const struct iphdr *)(mpls_hdr(skb) +
  120. label_index);
  121. if (v4hdr->version == 4) {
  122. hash = jhash_3words(ntohl(v4hdr->saddr),
  123. ntohl(v4hdr->daddr),
  124. v4hdr->protocol, hash);
  125. } else if (v4hdr->version == 6 &&
  126. pskb_may_pull(skb, sizeof(*hdr) * label_index +
  127. sizeof(struct ipv6hdr))) {
  128. const struct ipv6hdr *v6hdr;
  129. v6hdr = (const struct ipv6hdr *)(mpls_hdr(skb) +
  130. label_index);
  131. hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
  132. hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
  133. hash = jhash_1word(v6hdr->nexthdr, hash);
  134. }
  135. }
  136. }
  137. return hash;
  138. }
  139. static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
  140. struct sk_buff *skb, bool bos)
  141. {
  142. int alive = ACCESS_ONCE(rt->rt_nhn_alive);
  143. u32 hash = 0;
  144. int nh_index = 0;
  145. int n = 0;
  146. /* No need to look further into packet if there's only
  147. * one path
  148. */
  149. if (rt->rt_nhn == 1)
  150. goto out;
  151. if (alive <= 0)
  152. return NULL;
  153. hash = mpls_multipath_hash(rt, skb, bos);
  154. nh_index = hash % alive;
  155. if (alive == rt->rt_nhn)
  156. goto out;
  157. for_nexthops(rt) {
  158. if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  159. continue;
  160. if (n == nh_index)
  161. return nh;
  162. n++;
  163. } endfor_nexthops(rt);
  164. out:
  165. return &rt->rt_nh[nh_index];
  166. }
  167. static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
  168. struct mpls_entry_decoded dec)
  169. {
  170. enum mpls_payload_type payload_type;
  171. bool success = false;
  172. /* The IPv4 code below accesses through the IPv4 header
  173. * checksum, which is 12 bytes into the packet.
  174. * The IPv6 code below accesses through the IPv6 hop limit
  175. * which is 8 bytes into the packet.
  176. *
  177. * For all supported cases there should always be at least 12
  178. * bytes of packet data present. The IPv4 header is 20 bytes
  179. * without options and the IPv6 header is always 40 bytes
  180. * long.
  181. */
  182. if (!pskb_may_pull(skb, 12))
  183. return false;
  184. payload_type = rt->rt_payload_type;
  185. if (payload_type == MPT_UNSPEC)
  186. payload_type = ip_hdr(skb)->version;
  187. switch (payload_type) {
  188. case MPT_IPV4: {
  189. struct iphdr *hdr4 = ip_hdr(skb);
  190. skb->protocol = htons(ETH_P_IP);
  191. csum_replace2(&hdr4->check,
  192. htons(hdr4->ttl << 8),
  193. htons(dec.ttl << 8));
  194. hdr4->ttl = dec.ttl;
  195. success = true;
  196. break;
  197. }
  198. case MPT_IPV6: {
  199. struct ipv6hdr *hdr6 = ipv6_hdr(skb);
  200. skb->protocol = htons(ETH_P_IPV6);
  201. hdr6->hop_limit = dec.ttl;
  202. success = true;
  203. break;
  204. }
  205. case MPT_UNSPEC:
  206. break;
  207. }
  208. return success;
  209. }
  210. static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
  211. struct packet_type *pt, struct net_device *orig_dev)
  212. {
  213. struct net *net = dev_net(dev);
  214. struct mpls_shim_hdr *hdr;
  215. struct mpls_route *rt;
  216. struct mpls_nh *nh;
  217. struct mpls_entry_decoded dec;
  218. struct net_device *out_dev;
  219. struct mpls_dev *mdev;
  220. unsigned int hh_len;
  221. unsigned int new_header_size;
  222. unsigned int mtu;
  223. int err;
  224. /* Careful this entire function runs inside of an rcu critical section */
  225. mdev = mpls_dev_get(dev);
  226. if (!mdev || !mdev->input_enabled)
  227. goto drop;
  228. if (skb->pkt_type != PACKET_HOST)
  229. goto drop;
  230. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  231. goto drop;
  232. if (!pskb_may_pull(skb, sizeof(*hdr)))
  233. goto drop;
  234. /* Read and decode the label */
  235. hdr = mpls_hdr(skb);
  236. dec = mpls_entry_decode(hdr);
  237. /* Pop the label */
  238. skb_pull(skb, sizeof(*hdr));
  239. skb_reset_network_header(skb);
  240. skb_orphan(skb);
  241. rt = mpls_route_input_rcu(net, dec.label);
  242. if (!rt)
  243. goto drop;
  244. nh = mpls_select_multipath(rt, skb, dec.bos);
  245. if (!nh)
  246. goto drop;
  247. /* Find the output device */
  248. out_dev = rcu_dereference(nh->nh_dev);
  249. if (!mpls_output_possible(out_dev))
  250. goto drop;
  251. if (skb_warn_if_lro(skb))
  252. goto drop;
  253. skb_forward_csum(skb);
  254. /* Verify ttl is valid */
  255. if (dec.ttl <= 1)
  256. goto drop;
  257. dec.ttl -= 1;
  258. /* Verify the destination can hold the packet */
  259. new_header_size = mpls_nh_header_size(nh);
  260. mtu = mpls_dev_mtu(out_dev);
  261. if (mpls_pkt_too_big(skb, mtu - new_header_size))
  262. goto drop;
  263. hh_len = LL_RESERVED_SPACE(out_dev);
  264. if (!out_dev->header_ops)
  265. hh_len = 0;
  266. /* Ensure there is enough space for the headers in the skb */
  267. if (skb_cow(skb, hh_len + new_header_size))
  268. goto drop;
  269. skb->dev = out_dev;
  270. skb->protocol = htons(ETH_P_MPLS_UC);
  271. if (unlikely(!new_header_size && dec.bos)) {
  272. /* Penultimate hop popping */
  273. if (!mpls_egress(rt, skb, dec))
  274. goto drop;
  275. } else {
  276. bool bos;
  277. int i;
  278. skb_push(skb, new_header_size);
  279. skb_reset_network_header(skb);
  280. /* Push the new labels */
  281. hdr = mpls_hdr(skb);
  282. bos = dec.bos;
  283. for (i = nh->nh_labels - 1; i >= 0; i--) {
  284. hdr[i] = mpls_entry_encode(nh->nh_label[i],
  285. dec.ttl, 0, bos);
  286. bos = false;
  287. }
  288. }
  289. /* If via wasn't specified then send out using device address */
  290. if (nh->nh_via_table == MPLS_NEIGH_TABLE_UNSPEC)
  291. err = neigh_xmit(NEIGH_LINK_TABLE, out_dev,
  292. out_dev->dev_addr, skb);
  293. else
  294. err = neigh_xmit(nh->nh_via_table, out_dev,
  295. mpls_nh_via(rt, nh), skb);
  296. if (err)
  297. net_dbg_ratelimited("%s: packet transmission failed: %d\n",
  298. __func__, err);
  299. return 0;
  300. drop:
  301. kfree_skb(skb);
  302. return NET_RX_DROP;
  303. }
  304. static struct packet_type mpls_packet_type __read_mostly = {
  305. .type = cpu_to_be16(ETH_P_MPLS_UC),
  306. .func = mpls_forward,
  307. };
  308. static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
  309. [RTA_DST] = { .type = NLA_U32 },
  310. [RTA_OIF] = { .type = NLA_U32 },
  311. };
  312. struct mpls_route_config {
  313. u32 rc_protocol;
  314. u32 rc_ifindex;
  315. u8 rc_via_table;
  316. u8 rc_via_alen;
  317. u8 rc_via[MAX_VIA_ALEN];
  318. u32 rc_label;
  319. u8 rc_output_labels;
  320. u32 rc_output_label[MAX_NEW_LABELS];
  321. u32 rc_nlflags;
  322. enum mpls_payload_type rc_payload_type;
  323. struct nl_info rc_nlinfo;
  324. struct rtnexthop *rc_mp;
  325. int rc_mp_len;
  326. };
  327. static struct mpls_route *mpls_rt_alloc(int num_nh, u8 max_alen)
  328. {
  329. u8 max_alen_aligned = ALIGN(max_alen, VIA_ALEN_ALIGN);
  330. struct mpls_route *rt;
  331. rt = kzalloc(ALIGN(sizeof(*rt) + num_nh * sizeof(*rt->rt_nh),
  332. VIA_ALEN_ALIGN) +
  333. num_nh * max_alen_aligned,
  334. GFP_KERNEL);
  335. if (rt) {
  336. rt->rt_nhn = num_nh;
  337. rt->rt_nhn_alive = num_nh;
  338. rt->rt_max_alen = max_alen_aligned;
  339. }
  340. return rt;
  341. }
  342. static void mpls_rt_free(struct mpls_route *rt)
  343. {
  344. if (rt)
  345. kfree_rcu(rt, rt_rcu);
  346. }
  347. static void mpls_notify_route(struct net *net, unsigned index,
  348. struct mpls_route *old, struct mpls_route *new,
  349. const struct nl_info *info)
  350. {
  351. struct nlmsghdr *nlh = info ? info->nlh : NULL;
  352. unsigned portid = info ? info->portid : 0;
  353. int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
  354. struct mpls_route *rt = new ? new : old;
  355. unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
  356. /* Ignore reserved labels for now */
  357. if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
  358. rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
  359. }
  360. static void mpls_route_update(struct net *net, unsigned index,
  361. struct mpls_route *new,
  362. const struct nl_info *info)
  363. {
  364. struct mpls_route __rcu **platform_label;
  365. struct mpls_route *rt;
  366. ASSERT_RTNL();
  367. platform_label = rtnl_dereference(net->mpls.platform_label);
  368. rt = rtnl_dereference(platform_label[index]);
  369. rcu_assign_pointer(platform_label[index], new);
  370. mpls_notify_route(net, index, rt, new, info);
  371. /* If we removed a route free it now */
  372. mpls_rt_free(rt);
  373. }
  374. static unsigned find_free_label(struct net *net)
  375. {
  376. struct mpls_route __rcu **platform_label;
  377. size_t platform_labels;
  378. unsigned index;
  379. platform_label = rtnl_dereference(net->mpls.platform_label);
  380. platform_labels = net->mpls.platform_labels;
  381. for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
  382. index++) {
  383. if (!rtnl_dereference(platform_label[index]))
  384. return index;
  385. }
  386. return LABEL_NOT_SPECIFIED;
  387. }
  388. #if IS_ENABLED(CONFIG_INET)
  389. static struct net_device *inet_fib_lookup_dev(struct net *net,
  390. const void *addr)
  391. {
  392. struct net_device *dev;
  393. struct rtable *rt;
  394. struct in_addr daddr;
  395. memcpy(&daddr, addr, sizeof(struct in_addr));
  396. rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
  397. if (IS_ERR(rt))
  398. return ERR_CAST(rt);
  399. dev = rt->dst.dev;
  400. dev_hold(dev);
  401. ip_rt_put(rt);
  402. return dev;
  403. }
  404. #else
  405. static struct net_device *inet_fib_lookup_dev(struct net *net,
  406. const void *addr)
  407. {
  408. return ERR_PTR(-EAFNOSUPPORT);
  409. }
  410. #endif
  411. #if IS_ENABLED(CONFIG_IPV6)
  412. static struct net_device *inet6_fib_lookup_dev(struct net *net,
  413. const void *addr)
  414. {
  415. struct net_device *dev;
  416. struct dst_entry *dst;
  417. struct flowi6 fl6;
  418. int err;
  419. if (!ipv6_stub)
  420. return ERR_PTR(-EAFNOSUPPORT);
  421. memset(&fl6, 0, sizeof(fl6));
  422. memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
  423. err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6);
  424. if (err)
  425. return ERR_PTR(err);
  426. dev = dst->dev;
  427. dev_hold(dev);
  428. dst_release(dst);
  429. return dev;
  430. }
  431. #else
  432. static struct net_device *inet6_fib_lookup_dev(struct net *net,
  433. const void *addr)
  434. {
  435. return ERR_PTR(-EAFNOSUPPORT);
  436. }
  437. #endif
  438. static struct net_device *find_outdev(struct net *net,
  439. struct mpls_route *rt,
  440. struct mpls_nh *nh, int oif)
  441. {
  442. struct net_device *dev = NULL;
  443. if (!oif) {
  444. switch (nh->nh_via_table) {
  445. case NEIGH_ARP_TABLE:
  446. dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh));
  447. break;
  448. case NEIGH_ND_TABLE:
  449. dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh));
  450. break;
  451. case NEIGH_LINK_TABLE:
  452. break;
  453. }
  454. } else {
  455. dev = dev_get_by_index(net, oif);
  456. }
  457. if (!dev)
  458. return ERR_PTR(-ENODEV);
  459. /* The caller is holding rtnl anyways, so release the dev reference */
  460. dev_put(dev);
  461. return dev;
  462. }
  463. static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
  464. struct mpls_nh *nh, int oif)
  465. {
  466. struct net_device *dev = NULL;
  467. int err = -ENODEV;
  468. dev = find_outdev(net, rt, nh, oif);
  469. if (IS_ERR(dev)) {
  470. err = PTR_ERR(dev);
  471. dev = NULL;
  472. goto errout;
  473. }
  474. /* Ensure this is a supported device */
  475. err = -EINVAL;
  476. if (!mpls_dev_get(dev))
  477. goto errout;
  478. if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
  479. (dev->addr_len != nh->nh_via_alen))
  480. goto errout;
  481. RCU_INIT_POINTER(nh->nh_dev, dev);
  482. if (!(dev->flags & IFF_UP)) {
  483. nh->nh_flags |= RTNH_F_DEAD;
  484. } else {
  485. unsigned int flags;
  486. flags = dev_get_flags(dev);
  487. if (!(flags & (IFF_RUNNING | IFF_LOWER_UP)))
  488. nh->nh_flags |= RTNH_F_LINKDOWN;
  489. }
  490. return 0;
  491. errout:
  492. return err;
  493. }
  494. static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
  495. struct mpls_route *rt)
  496. {
  497. struct net *net = cfg->rc_nlinfo.nl_net;
  498. struct mpls_nh *nh = rt->rt_nh;
  499. int err;
  500. int i;
  501. if (!nh)
  502. return -ENOMEM;
  503. err = -EINVAL;
  504. /* Ensure only a supported number of labels are present */
  505. if (cfg->rc_output_labels > MAX_NEW_LABELS)
  506. goto errout;
  507. nh->nh_labels = cfg->rc_output_labels;
  508. for (i = 0; i < nh->nh_labels; i++)
  509. nh->nh_label[i] = cfg->rc_output_label[i];
  510. nh->nh_via_table = cfg->rc_via_table;
  511. memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
  512. nh->nh_via_alen = cfg->rc_via_alen;
  513. err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
  514. if (err)
  515. goto errout;
  516. if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  517. rt->rt_nhn_alive--;
  518. return 0;
  519. errout:
  520. return err;
  521. }
  522. static int mpls_nh_build(struct net *net, struct mpls_route *rt,
  523. struct mpls_nh *nh, int oif, struct nlattr *via,
  524. struct nlattr *newdst)
  525. {
  526. int err = -ENOMEM;
  527. if (!nh)
  528. goto errout;
  529. if (newdst) {
  530. err = nla_get_labels(newdst, MAX_NEW_LABELS,
  531. &nh->nh_labels, nh->nh_label);
  532. if (err)
  533. goto errout;
  534. }
  535. if (via) {
  536. err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
  537. __mpls_nh_via(rt, nh));
  538. if (err)
  539. goto errout;
  540. } else {
  541. nh->nh_via_table = MPLS_NEIGH_TABLE_UNSPEC;
  542. }
  543. err = mpls_nh_assign_dev(net, rt, nh, oif);
  544. if (err)
  545. goto errout;
  546. return 0;
  547. errout:
  548. return err;
  549. }
  550. static int mpls_count_nexthops(struct rtnexthop *rtnh, int len,
  551. u8 cfg_via_alen, u8 *max_via_alen)
  552. {
  553. int nhs = 0;
  554. int remaining = len;
  555. if (!rtnh) {
  556. *max_via_alen = cfg_via_alen;
  557. return 1;
  558. }
  559. *max_via_alen = 0;
  560. while (rtnh_ok(rtnh, remaining)) {
  561. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  562. int attrlen;
  563. attrlen = rtnh_attrlen(rtnh);
  564. nla = nla_find(attrs, attrlen, RTA_VIA);
  565. if (nla && nla_len(nla) >=
  566. offsetof(struct rtvia, rtvia_addr)) {
  567. int via_alen = nla_len(nla) -
  568. offsetof(struct rtvia, rtvia_addr);
  569. if (via_alen <= MAX_VIA_ALEN)
  570. *max_via_alen = max_t(u16, *max_via_alen,
  571. via_alen);
  572. }
  573. nhs++;
  574. rtnh = rtnh_next(rtnh, &remaining);
  575. }
  576. /* leftover implies invalid nexthop configuration, discard it */
  577. return remaining > 0 ? 0 : nhs;
  578. }
  579. static int mpls_nh_build_multi(struct mpls_route_config *cfg,
  580. struct mpls_route *rt)
  581. {
  582. struct rtnexthop *rtnh = cfg->rc_mp;
  583. struct nlattr *nla_via, *nla_newdst;
  584. int remaining = cfg->rc_mp_len;
  585. int nhs = 0;
  586. int err = 0;
  587. change_nexthops(rt) {
  588. int attrlen;
  589. nla_via = NULL;
  590. nla_newdst = NULL;
  591. err = -EINVAL;
  592. if (!rtnh_ok(rtnh, remaining))
  593. goto errout;
  594. /* neither weighted multipath nor any flags
  595. * are supported
  596. */
  597. if (rtnh->rtnh_hops || rtnh->rtnh_flags)
  598. goto errout;
  599. attrlen = rtnh_attrlen(rtnh);
  600. if (attrlen > 0) {
  601. struct nlattr *attrs = rtnh_attrs(rtnh);
  602. nla_via = nla_find(attrs, attrlen, RTA_VIA);
  603. nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
  604. }
  605. err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
  606. rtnh->rtnh_ifindex, nla_via, nla_newdst);
  607. if (err)
  608. goto errout;
  609. if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  610. rt->rt_nhn_alive--;
  611. rtnh = rtnh_next(rtnh, &remaining);
  612. nhs++;
  613. } endfor_nexthops(rt);
  614. rt->rt_nhn = nhs;
  615. return 0;
  616. errout:
  617. return err;
  618. }
  619. static int mpls_route_add(struct mpls_route_config *cfg)
  620. {
  621. struct mpls_route __rcu **platform_label;
  622. struct net *net = cfg->rc_nlinfo.nl_net;
  623. struct mpls_route *rt, *old;
  624. int err = -EINVAL;
  625. u8 max_via_alen;
  626. unsigned index;
  627. int nhs;
  628. index = cfg->rc_label;
  629. /* If a label was not specified during insert pick one */
  630. if ((index == LABEL_NOT_SPECIFIED) &&
  631. (cfg->rc_nlflags & NLM_F_CREATE)) {
  632. index = find_free_label(net);
  633. }
  634. /* Reserved labels may not be set */
  635. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  636. goto errout;
  637. /* The full 20 bit range may not be supported. */
  638. if (index >= net->mpls.platform_labels)
  639. goto errout;
  640. /* Append makes no sense with mpls */
  641. err = -EOPNOTSUPP;
  642. if (cfg->rc_nlflags & NLM_F_APPEND)
  643. goto errout;
  644. err = -EEXIST;
  645. platform_label = rtnl_dereference(net->mpls.platform_label);
  646. old = rtnl_dereference(platform_label[index]);
  647. if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
  648. goto errout;
  649. err = -EEXIST;
  650. if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
  651. goto errout;
  652. err = -ENOENT;
  653. if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
  654. goto errout;
  655. err = -EINVAL;
  656. nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
  657. cfg->rc_via_alen, &max_via_alen);
  658. if (nhs == 0)
  659. goto errout;
  660. err = -ENOMEM;
  661. rt = mpls_rt_alloc(nhs, max_via_alen);
  662. if (!rt)
  663. goto errout;
  664. rt->rt_protocol = cfg->rc_protocol;
  665. rt->rt_payload_type = cfg->rc_payload_type;
  666. if (cfg->rc_mp)
  667. err = mpls_nh_build_multi(cfg, rt);
  668. else
  669. err = mpls_nh_build_from_cfg(cfg, rt);
  670. if (err)
  671. goto freert;
  672. mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
  673. return 0;
  674. freert:
  675. mpls_rt_free(rt);
  676. errout:
  677. return err;
  678. }
  679. static int mpls_route_del(struct mpls_route_config *cfg)
  680. {
  681. struct net *net = cfg->rc_nlinfo.nl_net;
  682. unsigned index;
  683. int err = -EINVAL;
  684. index = cfg->rc_label;
  685. /* Reserved labels may not be removed */
  686. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  687. goto errout;
  688. /* The full 20 bit range may not be supported */
  689. if (index >= net->mpls.platform_labels)
  690. goto errout;
  691. mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
  692. err = 0;
  693. errout:
  694. return err;
  695. }
  696. #define MPLS_PERDEV_SYSCTL_OFFSET(field) \
  697. (&((struct mpls_dev *)0)->field)
  698. static const struct ctl_table mpls_dev_table[] = {
  699. {
  700. .procname = "input",
  701. .maxlen = sizeof(int),
  702. .mode = 0644,
  703. .proc_handler = proc_dointvec,
  704. .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
  705. },
  706. { }
  707. };
  708. static int mpls_dev_sysctl_register(struct net_device *dev,
  709. struct mpls_dev *mdev)
  710. {
  711. char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
  712. struct ctl_table *table;
  713. int i;
  714. table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
  715. if (!table)
  716. goto out;
  717. /* Table data contains only offsets relative to the base of
  718. * the mdev at this point, so make them absolute.
  719. */
  720. for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++)
  721. table[i].data = (char *)mdev + (uintptr_t)table[i].data;
  722. snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
  723. mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
  724. if (!mdev->sysctl)
  725. goto free;
  726. return 0;
  727. free:
  728. kfree(table);
  729. out:
  730. return -ENOBUFS;
  731. }
  732. static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
  733. {
  734. struct ctl_table *table;
  735. table = mdev->sysctl->ctl_table_arg;
  736. unregister_net_sysctl_table(mdev->sysctl);
  737. kfree(table);
  738. }
  739. static struct mpls_dev *mpls_add_dev(struct net_device *dev)
  740. {
  741. struct mpls_dev *mdev;
  742. int err = -ENOMEM;
  743. ASSERT_RTNL();
  744. mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
  745. if (!mdev)
  746. return ERR_PTR(err);
  747. err = mpls_dev_sysctl_register(dev, mdev);
  748. if (err)
  749. goto free;
  750. rcu_assign_pointer(dev->mpls_ptr, mdev);
  751. return mdev;
  752. free:
  753. kfree(mdev);
  754. return ERR_PTR(err);
  755. }
  756. static void mpls_ifdown(struct net_device *dev, int event)
  757. {
  758. struct mpls_route __rcu **platform_label;
  759. struct net *net = dev_net(dev);
  760. unsigned index;
  761. platform_label = rtnl_dereference(net->mpls.platform_label);
  762. for (index = 0; index < net->mpls.platform_labels; index++) {
  763. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  764. if (!rt)
  765. continue;
  766. change_nexthops(rt) {
  767. if (rtnl_dereference(nh->nh_dev) != dev)
  768. continue;
  769. switch (event) {
  770. case NETDEV_DOWN:
  771. case NETDEV_UNREGISTER:
  772. nh->nh_flags |= RTNH_F_DEAD;
  773. /* fall through */
  774. case NETDEV_CHANGE:
  775. nh->nh_flags |= RTNH_F_LINKDOWN;
  776. ACCESS_ONCE(rt->rt_nhn_alive) = rt->rt_nhn_alive - 1;
  777. break;
  778. }
  779. if (event == NETDEV_UNREGISTER)
  780. RCU_INIT_POINTER(nh->nh_dev, NULL);
  781. } endfor_nexthops(rt);
  782. }
  783. return;
  784. }
  785. static void mpls_ifup(struct net_device *dev, unsigned int nh_flags)
  786. {
  787. struct mpls_route __rcu **platform_label;
  788. struct net *net = dev_net(dev);
  789. unsigned index;
  790. int alive;
  791. platform_label = rtnl_dereference(net->mpls.platform_label);
  792. for (index = 0; index < net->mpls.platform_labels; index++) {
  793. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  794. if (!rt)
  795. continue;
  796. alive = 0;
  797. change_nexthops(rt) {
  798. struct net_device *nh_dev =
  799. rtnl_dereference(nh->nh_dev);
  800. if (!(nh->nh_flags & nh_flags)) {
  801. alive++;
  802. continue;
  803. }
  804. if (nh_dev != dev)
  805. continue;
  806. alive++;
  807. nh->nh_flags &= ~nh_flags;
  808. } endfor_nexthops(rt);
  809. ACCESS_ONCE(rt->rt_nhn_alive) = alive;
  810. }
  811. return;
  812. }
  813. static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
  814. void *ptr)
  815. {
  816. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  817. struct mpls_dev *mdev;
  818. unsigned int flags;
  819. if (event == NETDEV_REGISTER) {
  820. /* For now just support ethernet devices */
  821. if ((dev->type == ARPHRD_ETHER) ||
  822. (dev->type == ARPHRD_LOOPBACK)) {
  823. mdev = mpls_add_dev(dev);
  824. if (IS_ERR(mdev))
  825. return notifier_from_errno(PTR_ERR(mdev));
  826. }
  827. return NOTIFY_OK;
  828. }
  829. mdev = mpls_dev_get(dev);
  830. if (!mdev)
  831. return NOTIFY_OK;
  832. switch (event) {
  833. case NETDEV_DOWN:
  834. mpls_ifdown(dev, event);
  835. break;
  836. case NETDEV_UP:
  837. flags = dev_get_flags(dev);
  838. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  839. mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
  840. else
  841. mpls_ifup(dev, RTNH_F_DEAD);
  842. break;
  843. case NETDEV_CHANGE:
  844. flags = dev_get_flags(dev);
  845. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  846. mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
  847. else
  848. mpls_ifdown(dev, event);
  849. break;
  850. case NETDEV_UNREGISTER:
  851. mpls_ifdown(dev, event);
  852. mdev = mpls_dev_get(dev);
  853. if (mdev) {
  854. mpls_dev_sysctl_unregister(mdev);
  855. RCU_INIT_POINTER(dev->mpls_ptr, NULL);
  856. kfree_rcu(mdev, rcu);
  857. }
  858. break;
  859. case NETDEV_CHANGENAME:
  860. mdev = mpls_dev_get(dev);
  861. if (mdev) {
  862. int err;
  863. mpls_dev_sysctl_unregister(mdev);
  864. err = mpls_dev_sysctl_register(dev, mdev);
  865. if (err)
  866. return notifier_from_errno(err);
  867. }
  868. break;
  869. }
  870. return NOTIFY_OK;
  871. }
  872. static struct notifier_block mpls_dev_notifier = {
  873. .notifier_call = mpls_dev_notify,
  874. };
  875. static int nla_put_via(struct sk_buff *skb,
  876. u8 table, const void *addr, int alen)
  877. {
  878. static const int table_to_family[NEIGH_NR_TABLES + 1] = {
  879. AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
  880. };
  881. struct nlattr *nla;
  882. struct rtvia *via;
  883. int family = AF_UNSPEC;
  884. nla = nla_reserve(skb, RTA_VIA, alen + 2);
  885. if (!nla)
  886. return -EMSGSIZE;
  887. if (table <= NEIGH_NR_TABLES)
  888. family = table_to_family[table];
  889. via = nla_data(nla);
  890. via->rtvia_family = family;
  891. memcpy(via->rtvia_addr, addr, alen);
  892. return 0;
  893. }
  894. int nla_put_labels(struct sk_buff *skb, int attrtype,
  895. u8 labels, const u32 label[])
  896. {
  897. struct nlattr *nla;
  898. struct mpls_shim_hdr *nla_label;
  899. bool bos;
  900. int i;
  901. nla = nla_reserve(skb, attrtype, labels*4);
  902. if (!nla)
  903. return -EMSGSIZE;
  904. nla_label = nla_data(nla);
  905. bos = true;
  906. for (i = labels - 1; i >= 0; i--) {
  907. nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
  908. bos = false;
  909. }
  910. return 0;
  911. }
  912. EXPORT_SYMBOL_GPL(nla_put_labels);
  913. int nla_get_labels(const struct nlattr *nla,
  914. u32 max_labels, u8 *labels, u32 label[])
  915. {
  916. unsigned len = nla_len(nla);
  917. unsigned nla_labels;
  918. struct mpls_shim_hdr *nla_label;
  919. bool bos;
  920. int i;
  921. /* len needs to be an even multiple of 4 (the label size) */
  922. if (len & 3)
  923. return -EINVAL;
  924. /* Limit the number of new labels allowed */
  925. nla_labels = len/4;
  926. if (nla_labels > max_labels)
  927. return -EINVAL;
  928. nla_label = nla_data(nla);
  929. bos = true;
  930. for (i = nla_labels - 1; i >= 0; i--, bos = false) {
  931. struct mpls_entry_decoded dec;
  932. dec = mpls_entry_decode(nla_label + i);
  933. /* Ensure the bottom of stack flag is properly set
  934. * and ttl and tc are both clear.
  935. */
  936. if ((dec.bos != bos) || dec.ttl || dec.tc)
  937. return -EINVAL;
  938. switch (dec.label) {
  939. case MPLS_LABEL_IMPLNULL:
  940. /* RFC3032: This is a label that an LSR may
  941. * assign and distribute, but which never
  942. * actually appears in the encapsulation.
  943. */
  944. return -EINVAL;
  945. }
  946. label[i] = dec.label;
  947. }
  948. *labels = nla_labels;
  949. return 0;
  950. }
  951. EXPORT_SYMBOL_GPL(nla_get_labels);
  952. int nla_get_via(const struct nlattr *nla, u8 *via_alen,
  953. u8 *via_table, u8 via_addr[])
  954. {
  955. struct rtvia *via = nla_data(nla);
  956. int err = -EINVAL;
  957. int alen;
  958. if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
  959. goto errout;
  960. alen = nla_len(nla) -
  961. offsetof(struct rtvia, rtvia_addr);
  962. if (alen > MAX_VIA_ALEN)
  963. goto errout;
  964. /* Validate the address family */
  965. switch (via->rtvia_family) {
  966. case AF_PACKET:
  967. *via_table = NEIGH_LINK_TABLE;
  968. break;
  969. case AF_INET:
  970. *via_table = NEIGH_ARP_TABLE;
  971. if (alen != 4)
  972. goto errout;
  973. break;
  974. case AF_INET6:
  975. *via_table = NEIGH_ND_TABLE;
  976. if (alen != 16)
  977. goto errout;
  978. break;
  979. default:
  980. /* Unsupported address family */
  981. goto errout;
  982. }
  983. memcpy(via_addr, via->rtvia_addr, alen);
  984. *via_alen = alen;
  985. err = 0;
  986. errout:
  987. return err;
  988. }
  989. static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
  990. struct mpls_route_config *cfg)
  991. {
  992. struct rtmsg *rtm;
  993. struct nlattr *tb[RTA_MAX+1];
  994. int index;
  995. int err;
  996. err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
  997. if (err < 0)
  998. goto errout;
  999. err = -EINVAL;
  1000. rtm = nlmsg_data(nlh);
  1001. memset(cfg, 0, sizeof(*cfg));
  1002. if (rtm->rtm_family != AF_MPLS)
  1003. goto errout;
  1004. if (rtm->rtm_dst_len != 20)
  1005. goto errout;
  1006. if (rtm->rtm_src_len != 0)
  1007. goto errout;
  1008. if (rtm->rtm_tos != 0)
  1009. goto errout;
  1010. if (rtm->rtm_table != RT_TABLE_MAIN)
  1011. goto errout;
  1012. /* Any value is acceptable for rtm_protocol */
  1013. /* As mpls uses destination specific addresses
  1014. * (or source specific address in the case of multicast)
  1015. * all addresses have universal scope.
  1016. */
  1017. if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
  1018. goto errout;
  1019. if (rtm->rtm_type != RTN_UNICAST)
  1020. goto errout;
  1021. if (rtm->rtm_flags != 0)
  1022. goto errout;
  1023. cfg->rc_label = LABEL_NOT_SPECIFIED;
  1024. cfg->rc_protocol = rtm->rtm_protocol;
  1025. cfg->rc_via_table = MPLS_NEIGH_TABLE_UNSPEC;
  1026. cfg->rc_nlflags = nlh->nlmsg_flags;
  1027. cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
  1028. cfg->rc_nlinfo.nlh = nlh;
  1029. cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
  1030. for (index = 0; index <= RTA_MAX; index++) {
  1031. struct nlattr *nla = tb[index];
  1032. if (!nla)
  1033. continue;
  1034. switch(index) {
  1035. case RTA_OIF:
  1036. cfg->rc_ifindex = nla_get_u32(nla);
  1037. break;
  1038. case RTA_NEWDST:
  1039. if (nla_get_labels(nla, MAX_NEW_LABELS,
  1040. &cfg->rc_output_labels,
  1041. cfg->rc_output_label))
  1042. goto errout;
  1043. break;
  1044. case RTA_DST:
  1045. {
  1046. u8 label_count;
  1047. if (nla_get_labels(nla, 1, &label_count,
  1048. &cfg->rc_label))
  1049. goto errout;
  1050. /* Reserved labels may not be set */
  1051. if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED)
  1052. goto errout;
  1053. break;
  1054. }
  1055. case RTA_VIA:
  1056. {
  1057. if (nla_get_via(nla, &cfg->rc_via_alen,
  1058. &cfg->rc_via_table, cfg->rc_via))
  1059. goto errout;
  1060. break;
  1061. }
  1062. case RTA_MULTIPATH:
  1063. {
  1064. cfg->rc_mp = nla_data(nla);
  1065. cfg->rc_mp_len = nla_len(nla);
  1066. break;
  1067. }
  1068. default:
  1069. /* Unsupported attribute */
  1070. goto errout;
  1071. }
  1072. }
  1073. err = 0;
  1074. errout:
  1075. return err;
  1076. }
  1077. static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
  1078. {
  1079. struct mpls_route_config cfg;
  1080. int err;
  1081. err = rtm_to_route_config(skb, nlh, &cfg);
  1082. if (err < 0)
  1083. return err;
  1084. return mpls_route_del(&cfg);
  1085. }
  1086. static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
  1087. {
  1088. struct mpls_route_config cfg;
  1089. int err;
  1090. err = rtm_to_route_config(skb, nlh, &cfg);
  1091. if (err < 0)
  1092. return err;
  1093. return mpls_route_add(&cfg);
  1094. }
  1095. static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
  1096. u32 label, struct mpls_route *rt, int flags)
  1097. {
  1098. struct net_device *dev;
  1099. struct nlmsghdr *nlh;
  1100. struct rtmsg *rtm;
  1101. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
  1102. if (nlh == NULL)
  1103. return -EMSGSIZE;
  1104. rtm = nlmsg_data(nlh);
  1105. rtm->rtm_family = AF_MPLS;
  1106. rtm->rtm_dst_len = 20;
  1107. rtm->rtm_src_len = 0;
  1108. rtm->rtm_tos = 0;
  1109. rtm->rtm_table = RT_TABLE_MAIN;
  1110. rtm->rtm_protocol = rt->rt_protocol;
  1111. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  1112. rtm->rtm_type = RTN_UNICAST;
  1113. rtm->rtm_flags = 0;
  1114. if (nla_put_labels(skb, RTA_DST, 1, &label))
  1115. goto nla_put_failure;
  1116. if (rt->rt_nhn == 1) {
  1117. const struct mpls_nh *nh = rt->rt_nh;
  1118. if (nh->nh_labels &&
  1119. nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
  1120. nh->nh_label))
  1121. goto nla_put_failure;
  1122. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1123. nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
  1124. nh->nh_via_alen))
  1125. goto nla_put_failure;
  1126. dev = rtnl_dereference(nh->nh_dev);
  1127. if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
  1128. goto nla_put_failure;
  1129. if (nh->nh_flags & RTNH_F_LINKDOWN)
  1130. rtm->rtm_flags |= RTNH_F_LINKDOWN;
  1131. if (nh->nh_flags & RTNH_F_DEAD)
  1132. rtm->rtm_flags |= RTNH_F_DEAD;
  1133. } else {
  1134. struct rtnexthop *rtnh;
  1135. struct nlattr *mp;
  1136. int dead = 0;
  1137. int linkdown = 0;
  1138. mp = nla_nest_start(skb, RTA_MULTIPATH);
  1139. if (!mp)
  1140. goto nla_put_failure;
  1141. for_nexthops(rt) {
  1142. rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
  1143. if (!rtnh)
  1144. goto nla_put_failure;
  1145. dev = rtnl_dereference(nh->nh_dev);
  1146. if (dev)
  1147. rtnh->rtnh_ifindex = dev->ifindex;
  1148. if (nh->nh_flags & RTNH_F_LINKDOWN) {
  1149. rtnh->rtnh_flags |= RTNH_F_LINKDOWN;
  1150. linkdown++;
  1151. }
  1152. if (nh->nh_flags & RTNH_F_DEAD) {
  1153. rtnh->rtnh_flags |= RTNH_F_DEAD;
  1154. dead++;
  1155. }
  1156. if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
  1157. nh->nh_labels,
  1158. nh->nh_label))
  1159. goto nla_put_failure;
  1160. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1161. nla_put_via(skb, nh->nh_via_table,
  1162. mpls_nh_via(rt, nh),
  1163. nh->nh_via_alen))
  1164. goto nla_put_failure;
  1165. /* length of rtnetlink header + attributes */
  1166. rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
  1167. } endfor_nexthops(rt);
  1168. if (linkdown == rt->rt_nhn)
  1169. rtm->rtm_flags |= RTNH_F_LINKDOWN;
  1170. if (dead == rt->rt_nhn)
  1171. rtm->rtm_flags |= RTNH_F_DEAD;
  1172. nla_nest_end(skb, mp);
  1173. }
  1174. nlmsg_end(skb, nlh);
  1175. return 0;
  1176. nla_put_failure:
  1177. nlmsg_cancel(skb, nlh);
  1178. return -EMSGSIZE;
  1179. }
  1180. static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
  1181. {
  1182. struct net *net = sock_net(skb->sk);
  1183. struct mpls_route __rcu **platform_label;
  1184. size_t platform_labels;
  1185. unsigned int index;
  1186. ASSERT_RTNL();
  1187. index = cb->args[0];
  1188. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  1189. index = MPLS_LABEL_FIRST_UNRESERVED;
  1190. platform_label = rtnl_dereference(net->mpls.platform_label);
  1191. platform_labels = net->mpls.platform_labels;
  1192. for (; index < platform_labels; index++) {
  1193. struct mpls_route *rt;
  1194. rt = rtnl_dereference(platform_label[index]);
  1195. if (!rt)
  1196. continue;
  1197. if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
  1198. cb->nlh->nlmsg_seq, RTM_NEWROUTE,
  1199. index, rt, NLM_F_MULTI) < 0)
  1200. break;
  1201. }
  1202. cb->args[0] = index;
  1203. return skb->len;
  1204. }
  1205. static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
  1206. {
  1207. size_t payload =
  1208. NLMSG_ALIGN(sizeof(struct rtmsg))
  1209. + nla_total_size(4); /* RTA_DST */
  1210. if (rt->rt_nhn == 1) {
  1211. struct mpls_nh *nh = rt->rt_nh;
  1212. if (nh->nh_dev)
  1213. payload += nla_total_size(4); /* RTA_OIF */
  1214. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */
  1215. payload += nla_total_size(2 + nh->nh_via_alen);
  1216. if (nh->nh_labels) /* RTA_NEWDST */
  1217. payload += nla_total_size(nh->nh_labels * 4);
  1218. } else {
  1219. /* each nexthop is packed in an attribute */
  1220. size_t nhsize = 0;
  1221. for_nexthops(rt) {
  1222. nhsize += nla_total_size(sizeof(struct rtnexthop));
  1223. /* RTA_VIA */
  1224. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
  1225. nhsize += nla_total_size(2 + nh->nh_via_alen);
  1226. if (nh->nh_labels)
  1227. nhsize += nla_total_size(nh->nh_labels * 4);
  1228. } endfor_nexthops(rt);
  1229. /* nested attribute */
  1230. payload += nla_total_size(nhsize);
  1231. }
  1232. return payload;
  1233. }
  1234. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  1235. struct nlmsghdr *nlh, struct net *net, u32 portid,
  1236. unsigned int nlm_flags)
  1237. {
  1238. struct sk_buff *skb;
  1239. u32 seq = nlh ? nlh->nlmsg_seq : 0;
  1240. int err = -ENOBUFS;
  1241. skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
  1242. if (skb == NULL)
  1243. goto errout;
  1244. err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
  1245. if (err < 0) {
  1246. /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
  1247. WARN_ON(err == -EMSGSIZE);
  1248. kfree_skb(skb);
  1249. goto errout;
  1250. }
  1251. rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
  1252. return;
  1253. errout:
  1254. if (err < 0)
  1255. rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
  1256. }
  1257. static int resize_platform_label_table(struct net *net, size_t limit)
  1258. {
  1259. size_t size = sizeof(struct mpls_route *) * limit;
  1260. size_t old_limit;
  1261. size_t cp_size;
  1262. struct mpls_route __rcu **labels = NULL, **old;
  1263. struct mpls_route *rt0 = NULL, *rt2 = NULL;
  1264. unsigned index;
  1265. if (size) {
  1266. labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
  1267. if (!labels)
  1268. labels = vzalloc(size);
  1269. if (!labels)
  1270. goto nolabels;
  1271. }
  1272. /* In case the predefined labels need to be populated */
  1273. if (limit > MPLS_LABEL_IPV4NULL) {
  1274. struct net_device *lo = net->loopback_dev;
  1275. rt0 = mpls_rt_alloc(1, lo->addr_len);
  1276. if (!rt0)
  1277. goto nort0;
  1278. RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
  1279. rt0->rt_protocol = RTPROT_KERNEL;
  1280. rt0->rt_payload_type = MPT_IPV4;
  1281. rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  1282. rt0->rt_nh->nh_via_alen = lo->addr_len;
  1283. memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
  1284. lo->addr_len);
  1285. }
  1286. if (limit > MPLS_LABEL_IPV6NULL) {
  1287. struct net_device *lo = net->loopback_dev;
  1288. rt2 = mpls_rt_alloc(1, lo->addr_len);
  1289. if (!rt2)
  1290. goto nort2;
  1291. RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
  1292. rt2->rt_protocol = RTPROT_KERNEL;
  1293. rt2->rt_payload_type = MPT_IPV6;
  1294. rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  1295. rt2->rt_nh->nh_via_alen = lo->addr_len;
  1296. memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
  1297. lo->addr_len);
  1298. }
  1299. rtnl_lock();
  1300. /* Remember the original table */
  1301. old = rtnl_dereference(net->mpls.platform_label);
  1302. old_limit = net->mpls.platform_labels;
  1303. /* Free any labels beyond the new table */
  1304. for (index = limit; index < old_limit; index++)
  1305. mpls_route_update(net, index, NULL, NULL);
  1306. /* Copy over the old labels */
  1307. cp_size = size;
  1308. if (old_limit < limit)
  1309. cp_size = old_limit * sizeof(struct mpls_route *);
  1310. memcpy(labels, old, cp_size);
  1311. /* If needed set the predefined labels */
  1312. if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
  1313. (limit > MPLS_LABEL_IPV6NULL)) {
  1314. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
  1315. rt2 = NULL;
  1316. }
  1317. if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
  1318. (limit > MPLS_LABEL_IPV4NULL)) {
  1319. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
  1320. rt0 = NULL;
  1321. }
  1322. /* Update the global pointers */
  1323. net->mpls.platform_labels = limit;
  1324. rcu_assign_pointer(net->mpls.platform_label, labels);
  1325. rtnl_unlock();
  1326. mpls_rt_free(rt2);
  1327. mpls_rt_free(rt0);
  1328. if (old) {
  1329. synchronize_rcu();
  1330. kvfree(old);
  1331. }
  1332. return 0;
  1333. nort2:
  1334. mpls_rt_free(rt0);
  1335. nort0:
  1336. kvfree(labels);
  1337. nolabels:
  1338. return -ENOMEM;
  1339. }
  1340. static int mpls_platform_labels(struct ctl_table *table, int write,
  1341. void __user *buffer, size_t *lenp, loff_t *ppos)
  1342. {
  1343. struct net *net = table->data;
  1344. int platform_labels = net->mpls.platform_labels;
  1345. int ret;
  1346. struct ctl_table tmp = {
  1347. .procname = table->procname,
  1348. .data = &platform_labels,
  1349. .maxlen = sizeof(int),
  1350. .mode = table->mode,
  1351. .extra1 = &zero,
  1352. .extra2 = &label_limit,
  1353. };
  1354. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  1355. if (write && ret == 0)
  1356. ret = resize_platform_label_table(net, platform_labels);
  1357. return ret;
  1358. }
  1359. static const struct ctl_table mpls_table[] = {
  1360. {
  1361. .procname = "platform_labels",
  1362. .data = NULL,
  1363. .maxlen = sizeof(int),
  1364. .mode = 0644,
  1365. .proc_handler = mpls_platform_labels,
  1366. },
  1367. { }
  1368. };
  1369. static int mpls_net_init(struct net *net)
  1370. {
  1371. struct ctl_table *table;
  1372. net->mpls.platform_labels = 0;
  1373. net->mpls.platform_label = NULL;
  1374. table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
  1375. if (table == NULL)
  1376. return -ENOMEM;
  1377. table[0].data = net;
  1378. net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
  1379. if (net->mpls.ctl == NULL) {
  1380. kfree(table);
  1381. return -ENOMEM;
  1382. }
  1383. return 0;
  1384. }
  1385. static void mpls_net_exit(struct net *net)
  1386. {
  1387. struct mpls_route __rcu **platform_label;
  1388. size_t platform_labels;
  1389. struct ctl_table *table;
  1390. unsigned int index;
  1391. table = net->mpls.ctl->ctl_table_arg;
  1392. unregister_net_sysctl_table(net->mpls.ctl);
  1393. kfree(table);
  1394. /* An rcu grace period has passed since there was a device in
  1395. * the network namespace (and thus the last in flight packet)
  1396. * left this network namespace. This is because
  1397. * unregister_netdevice_many and netdev_run_todo has completed
  1398. * for each network device that was in this network namespace.
  1399. *
  1400. * As such no additional rcu synchronization is necessary when
  1401. * freeing the platform_label table.
  1402. */
  1403. rtnl_lock();
  1404. platform_label = rtnl_dereference(net->mpls.platform_label);
  1405. platform_labels = net->mpls.platform_labels;
  1406. for (index = 0; index < platform_labels; index++) {
  1407. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  1408. RCU_INIT_POINTER(platform_label[index], NULL);
  1409. mpls_rt_free(rt);
  1410. }
  1411. rtnl_unlock();
  1412. kvfree(platform_label);
  1413. }
  1414. static struct pernet_operations mpls_net_ops = {
  1415. .init = mpls_net_init,
  1416. .exit = mpls_net_exit,
  1417. };
  1418. static int __init mpls_init(void)
  1419. {
  1420. int err;
  1421. BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
  1422. err = register_pernet_subsys(&mpls_net_ops);
  1423. if (err)
  1424. goto out;
  1425. err = register_netdevice_notifier(&mpls_dev_notifier);
  1426. if (err)
  1427. goto out_unregister_pernet;
  1428. dev_add_pack(&mpls_packet_type);
  1429. rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
  1430. rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
  1431. rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
  1432. err = 0;
  1433. out:
  1434. return err;
  1435. out_unregister_pernet:
  1436. unregister_pernet_subsys(&mpls_net_ops);
  1437. goto out;
  1438. }
  1439. module_init(mpls_init);
  1440. static void __exit mpls_exit(void)
  1441. {
  1442. rtnl_unregister_all(PF_MPLS);
  1443. dev_remove_pack(&mpls_packet_type);
  1444. unregister_netdevice_notifier(&mpls_dev_notifier);
  1445. unregister_pernet_subsys(&mpls_net_ops);
  1446. }
  1447. module_exit(mpls_exit);
  1448. MODULE_DESCRIPTION("MultiProtocol Label Switching");
  1449. MODULE_LICENSE("GPL v2");
  1450. MODULE_ALIAS_NETPROTO(PF_MPLS);