af_mpls.c 39 KB

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